Re: How to serialize and deserialize QueryDSL Expressions?

2,463 views
Skip to first unread message

Timo Westkämper

unread,
Jan 2, 2013, 4:25:19 AM1/2/13
to Querydsl on behalf of René Reitmann
Hi.

Which Querydsl module are you mainly using? If you are using JPA and don't aim for a general approach I'd just serialize the Querydsl queries to JPQL and store the serialized queries and the parameter bindings.

Querydsl queries can also be serialized into binary form.

Br,
Timo

On Wed, Jan 2, 2013 at 11:09 AM, René Reitmann via Querydsl <querydsl+noreply-APn2wQeNoDERzkU...@googlegroups.com> wrote:
Hi,

I would like to serialize QueryDSL Expressions into a human readable (refactorable) String including all parameters and store it in a DB in order to execute the query "later". Calling toString() on a BooleanExpression for instance seems to be a perfect match. However, I do not find a way to create a BooleanExpression from that String meaning I am not able to deserialize the QueryDSL Expression. Has someone tried something similar already?

Regards,

René



--
Timo Westkämper
Mysema Oy
+358 (0)40 591 2172
www.mysema.com


René Reitmann

unread,
Jan 2, 2013, 4:43:51 AM1/2/13
to quer...@googlegroups.com
Hi Timo,

thanks for the immediate response.

We are using mainly JPA indeed. However I would favor a more general approach in order to be able to switch to MongoDB for instance. Serializing/Deserializing the QueryDSL Pojos is not a good option either since refactoring entities would cause pretty much effort since we would need to refactor the serialized Pojos as well. That's the reason why I asked for a way to deserialize the String created by toString(). Refactoring that String should be simple :-)

Br,

René

Am Mittwoch, 2. Januar 2013 10:25:19 UTC+1 schrieb Timo Westkämper:
Hi.

Which Querydsl module are you mainly using? If you are using JPA and don't aim for a general approach I'd just serialize the Querydsl queries to JPQL and store the serialized queries and the parameter bindings.

Querydsl queries can also be serialized into binary form.

Br,
Timo

Timo Westkämper

unread,
Jan 2, 2013, 4:48:37 AM1/2/13
to Querydsl on behalf of René Reitmann
Hi Rene.

There is not yet a parser for Querydsl expressions, but I could provide some guidance, if you are interested in contributing one.

You can either emulate SQL/JPQL in the serialization, or just dump the AST into XML, JSON or some custom tree format, which is much easier. Supporting operations, paths and constants should be enough for quite a while.

Br,
Timo

René Reitmann

unread,
Jan 2, 2013, 5:14:52 AM1/2/13
to quer...@googlegroups.com
Hi again,

I think I go with the JPQLSerializer for the moment. If there is some time left I will give the JSON idea a try.

Regards,

René


Am Mittwoch, 2. Januar 2013 10:48:37 UTC+1 schrieb Timo Westkämper:
Hi Rene.

There is not yet a parser for Querydsl expressions, but I could provide some guidance, if you are interested in contributing one.

You can either emulate SQL/JPQL in the serialization, or just dump the AST into XML, JSON or some custom tree format, which is much easier. Supporting operations, paths and constants should be enough for quite a while.

Br,
Timo

Joshua Chaitin-Pollak

unread,
Apr 7, 2015, 5:01:56 PM4/7/15
to quer...@googlegroups.com
Hello,

Sorry to revive an old thread. Has there been any progress on serializing the AST to JSON?

I think this would be extremely useful is to allow queries to be rendered and edited in a UI and re-executed.

alexand...@a-sis.com

unread,
May 11, 2015, 4:36:52 AM5/11/15
to quer...@googlegroups.com
Hello,

Like Joshua, i am also very interesting for serializing queryDSL expression into string or json. Is there any update about this functionality ?

Please René, could you tell us how you solve it ?

Thanks for your answer.

timowest

unread,
May 11, 2015, 4:51:19 PM5/11/15
to quer...@googlegroups.com
JSON serialization/parsing functionality is not planned.

If someone wants to implement it, I could advise how to start it.

Alex Escot

unread,
May 12, 2015, 3:30:21 AM5/12/15
to quer...@googlegroups.com
Hi Timo,

Thanks for your quick answer.

I have to implement a JSON serializer/deserializer for my company project because we can configure predicates and orders into a web UI. 
Our needs is just to serialize predicates and orders expressions and not a complete query but if it could help the project maybe i can do the complete job.

Concerning the serializer, we planned to have a JSON representation of the query that is very similar to MongoDB because criteria are pretty easy to read. So the code will be inspired of MongodbSerializer

Concerning the deserializer, we planned to use the PathBuilder class for recreate the path expression. And then use the class ExpressionUtils for create all predicates.
 I think the deserializer/parser is a more complex.

What do you think about this ? Please, could you give me your advices.

timowest

unread,
May 12, 2015, 11:52:02 AM5/12/15
to quer...@googlegroups.com
Hi.

Concerning serialization I'd just use a visitor

Just return a presentation that you can directly serialize into jackson (map or bean).

For parsing it's the inverse and you can use Expressions to construct the expressions.

Enums might be tricky, also Operator, maybe you can start with operations, paths and constants and then extend it, if necessary.

Timo

Alex Escot

unread,
May 12, 2015, 1:30:11 PM5/12/15
to quer...@googlegroups.com
Hi,

Thanks for tour answer. It will be very usefull.

Concerning the deserializer, I have an issue with the type of the field. I don't know how i can get the type of the field path in order to transform the constant value in the same type.

When I get the path with the class PathBuilder, the method getType return me a Object type.

Please, Do you know how i can solve it ?

timowest

unread,
May 12, 2015, 3:02:37 PM5/12/15
to quer...@googlegroups.com
How do you construct the PathBuilder?

Alex Escot

unread,
May 12, 2015, 7:14:21 PM5/12/15
to quer...@googlegroups.com
I have a JSON predicate formatting like that : { "content.product.quantity" : "100" } 
I want to dynamically construct this predicate : QContent.content.product.quantity.eq(100)

I build the PathBuilder of my root element : PathBuilder<Content> entityPath = new PathBuilder<Content>(Content.class, "content"); 

And I split the json string path on the character "." and call the get(String attribute) method on each element. the last element give me the PathBuilder of my field quantity.
But i am using a full generic access (I don't give any type) so I get a PathBuilder which is not correctly typed.

Maybe, there is a better way for doing this ?

Thanks for your help. 

Alex Escot

unread,
May 13, 2015, 3:46:54 AM5/13/15
to quer...@googlegroups.com
You can see below an unit test that fails and shows how the path is built.

@Test
public void should_construct_dynamic_path() {
PathBuilder path = new PathBuilder<Content>(Content.class, QContent.content.getMetadata());
String str = "product.price";
for (String element : str.split("\\.")) {
path = path.get(element);
}
assertThat(path).isEqualTo(QContent.content.product.price); // OK
assertThat(path.getType()).isEqualTo(Integer.class); // NOK
}

Le mardi 12 mai 2015 21:02:37 UTC+2, timowest a écrit :

Ruben Dijkstra

unread,
May 13, 2015, 3:58:30 AM5/13/15
to quer...@googlegroups.com, Alex Escot via Querydsl
Hello,

What you can do if you must match the property types, is pass the specific PathBuilderValidator parameter.

By default we do not validate the property and it's type, but you can specify PathBuilderValidator.FIELDS to validate the raw field type and PathBuilderValidator.PROPERTIES to validate using the getter.

I hope this helps.

Br,

Ruben
--
You received this message because you are subscribed to the Google Groups "Querydsl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to querydsl+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



Alex Escot

unread,
May 13, 2015, 4:35:01 AM5/13/15
to quer...@googlegroups.com
Hello Ruben,

Thanks for your answer, it solves my issue. I now have the field type, and I can easily transform the json value into the same type.

Br,


Le mercredi 13 mai 2015 09:58:30 UTC+2, Ruben Dijkstra a écrit :
Hello,

What you can do if you must match the property types, is pass the specific PathBuilderValidator parameter.

By default we do not validate the property and it's type, but you can specify PathBuilderValidator.FIELDS to validate the raw field type and PathBuilderValidator.PROPERTIES to validate using the getter.

I hope this helps.

Br,

Ruben

Romuald MAIRE

unread,
May 20, 2015, 6:02:53 AM5/20/15
to quer...@googlegroups.com
If someone already did that, is it possible to have the serializer/deserializer using JSON please ? ^^

Romuald MAIRE

unread,
May 20, 2015, 9:03:56 AM5/20/15
to quer...@googlegroups.com
Or maybe it is just possible to make the interface "com.mysema.query.types.path.PathBuilderValidator" to extends "Serializable" please ?

 I just need to serialize a Predicate and an OrderSpecifier ...

Sebastian Świerczyna

unread,
May 4, 2016, 11:42:16 AM5/4/16
to Querydsl
Hello,

I'am also interested how  to send query over http using JSON.
Anyone ?

Alex Escot

unread,
May 6, 2016, 8:45:44 AM5/6/16
to Querydsl
If it could help you, I send you a gist of my JSON serializer / deserializer with associated unit tests.

But it only serialize deserialize Predicate and OrderSpecifier, i don't need more...

It has been developped according Timo advices : using Visitor for serialization and PathBuilder for deserialization,

Maybe, it could help the community.


My syntax is pretty the same as MongoDB.

JWheat

unread,
Feb 24, 2017, 2:50:16 PM2/24/17
to Querydsl
Are the com.asis.kis import classes required for DefaultQueryDslDeserializer and DefaultQueryDslSerializer available for download? 

JWheat

unread,
May 7, 2017, 11:19:17 AM5/7/17
to Querydsl

Here is a solution that translates a RSQL string into a QueryDSL predicate. RSQL-QueryDSL

Ahul

unread,
Nov 27, 2017, 7:49:11 AM11/27/17
to Querydsl
Hi Scot,

I used this code sample , but getting problems on root class containg complex objects.

Ahul K.Vidhyadaran

unread,
Nov 30, 2017, 1:42:35 AM11/30/17
to Querydsl on behalf of Ahul

Hi,

Any one done serialiser / deserializer for predicate containing complex objects?

--
You received this message because you are subscribed to a topic in the Google Groups "Querydsl" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/querydsl/p3MFCF1EpjM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to querydsl+unsubscribe@googlegroups.com.

Alex Escot

unread,
Nov 30, 2017, 2:29:10 AM11/30/17
to Querydsl
Hi,

Could you please give us more information about your issue, do you have an example ?

Because we are using this serializer/deserialier in our project for more than two years without any issue (As you could see in unit tests, it permits to navigate behind complex complex object).

Le jeudi 30 novembre 2017 07:42:35 UTC+1, Ahul a écrit :

Hi,

Any one done serialiser / deserializer for predicate containing complex objects?

Hi Scot,

I used this code sample , but getting problems on root class containg complex objects.

On Friday, May 6, 2016 at 6:15:44 PM UTC+5:30, Alex Escot wrote:
If it could help you, I send you a gist of my JSON serializer / deserializer with associated unit tests.

But it only serialize deserialize Predicate and OrderSpecifier, i don't need more...

It has been developped according Timo advices : using Visitor for serialization and PathBuilder for deserialization,

Maybe, it could help the community.


My syntax is pretty the same as MongoDB.

Le mercredi 4 mai 2016 17:42:16 UTC+2, Sebastian Świerczyna a écrit :
Hello,

I'am also interested how  to send query over http using JSON.
Anyone ?

W dniu środa, 20 maja 2015 12:02:53 UTC+2 użytkownik Romuald MAIRE napisał:
If someone already did that, is it possible to have the serializer/deserializer using JSON please ? ^^

--
You received this message because you are subscribed to a topic in the Google Groups "Querydsl" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/querydsl/p3MFCF1EpjM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to querydsl+u...@googlegroups.com.

Ahul K.Vidhyadaran

unread,
Nov 30, 2017, 3:24:44 AM11/30/17
to quer...@googlegroups.com

Hi,
I have attached root class and generated Q class, I'm getting problem on Set type in root class, also tried it with List as well.
Thanks

To unsubscribe from this group and all its topics, send an email to querydsl+unsubscribe@googlegroups.com.

Ahul K.Vidhyadaran

unread,
Nov 30, 2017, 5:05:41 AM11/30/17
to quer...@googlegroups.com

Hi,
Please see the root class and Q class

On 30-Nov-2017 1:54 PM, "Ahul K.Vidhyadaran" <ahulk.vi...@gmail.com> wrote:

Hi,
I have attached root class and generated Q class, I'm getting problem on Set type in root class, also tried it with List as well.
Thanks

On 30-Nov-2017 12:59 PM, "Alex Escot via Querydsl" <querydsl+APn2wQdol38tEJKmOwcoWZlyKa7dvpeqm6ttFzD14gY2INZGV20YdJc@googlegroups.com> wrote:
Hi,

Could you please give us more information about your issue, do you have an example ?

Because we are using this serializer/deserialier in our project for more than two years without any issue (As you could see in unit tests, it permits to navigate behind complex complex object).

Le jeudi 30 novembre 2017 07:42:35 UTC+1, Ahul a écrit :

Hi,

Any one done serialiser / deserializer for predicate containing complex objects?

AccountDE.PNG
QAccountDE.PNG
Adress.PNG
Reply all
Reply to author
Forward
0 new messages