Ebean Model to JSON conversion

350 views
Skip to first unread message

Mayur V Vijaywargi

unread,
May 5, 2019, 2:25:38 AM5/5/19
to Ebean ORM
I am trying to write a generic api which will take the fields required to fetch from the model and return it as response. This also contains related models and their fields. If I use Json.toJson of play framework it is loading all the properties and all the related models. Response of the api is in snake case. This is done by setting object mapper properties.

On using `JsonContext jsonContext = Ebean.json();`,  and converting it to json string using `jsonContext.toJson` returns you json in camel case. I tried using JsonWriteOptions and setting the object mapper of it to use snake case conversion but was of no use.

Can someone help me out for converting the ebean models to snake case json string using JsonContext of Ebean.

Rob Bygrave

unread,
May 7, 2019, 6:10:52 AM5/7/19
to ebean@googlegroups
> I am trying to write a generic api which will take the fields required to fetch from the model and return it as response

Note that Ebean PathProperties potentially does something like this.  It uses the linkedin dsl/format to define what part of the object graph should be queried and returned in the JSON response.
e.g.

PathProperties pathProps = PathProperties.parse("id,name,billingAddress(city),shippingAddress(*))");

Query<Customer> query = DB.find(Customer.class)
.where().lt("id", 2)
.query();

pathProps.apply(query);

List<Customer> list = query.findList();

String asJson = DB.json().toJson(list, pathProps);


GROFERS

Download App

Follow us: Facebook :: Twitter :: Instagram



--

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

Mayur V Vijaywargi

unread,
May 7, 2019, 6:37:07 AM5/7/19
to Ebean ORM
The problem is we are using object mapper which convert keys to snake case and time stamp to ISO format. Somehow the json context is not using the object mapper passed and hence we are getting snakecase key json response with datetimefield in millis
To unsubscribe from this group and stop receiving emails from it, send an email to eb...@googlegroups.com.

Rob Bygrave

unread,
May 7, 2019, 8:43:10 AM5/7/19
to ebean@googlegroups
 > using object mapper which convert keys to snake case

Ebean is using JsonGenerator writing the keys.  Have a look at BeanDescriptorJsonHelp.



> and time stamp to ISO format.

See DatabaseConfig / ServerConfig .setJsonDateTime(JsonConfig.DateTime.ISO8601).



> using object mapper which convert keys to snake case and time stamp to ISO format.

Ebean does not use ObjectMapper for marshalling the entity beans to JSON per say but instead uses Jackson's JsonGenerator (and JsonParser) and uses them on a per property basis.  It does this for many reasons including the need to support partially loaded beans etc. 

Ebean uses ObjectMapper for property types that it can't/doesn't use JsonGenerator for (like plain java bean types mapped to Postgres JSON/JSONB columns etc).




To unsubscribe from this group and stop receiving emails from it, send an email to ebean+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ebean/6e4e5b1a-5787-4f28-b244-fbeea0e49b10%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages