Elegant mapping from POJOs to vertx.io's JsonObject?
Asked Answered
E

6

29

I am currently working on a vertx.io application and wanted to use the provide mongo api for data storage. I currently have a rather clunky abstraction on top of the stock JsonObject classes where all get and set methods are replaced with things like:

this.backingObject.get(KEY_FOR_THIS_PROPERTY);

This is all well and good for now, but it won't scale particularly well. it also seems dirty, specifically when using nested arrays or objects. For example, if I want to be able to fill fields only when actual data is known, I have to check if the array exists, and if it doesn't create it and store it in the object. Then I can add an element to the list. For example:

if (this.backingObject.getJsonArray(KEY_LIST) == null) {
    this.backingObject.put(KEY_LIST, new JsonArray());
}
this.backingObject.getJsonArray(KEY_LIST).add(p.getBackingObject());

I have thought about potential solutions but don't particularly like any of them. Namely, I could use Gson or some similar library with annotation support to handle loading the object for the purposes of manipulating the data in my code, and then using the serialize and unserialize function of both Gson and Vertx to convert between the formats (vertx to load data -> json string -> gson to parse json into pojos -> make changes -> serialize to json string -> parse with vertx and save) but that's a really gross and inefficient workflow. I could also probably come up with some sort of abstract wrapper that extends/implements the vertx json library but passes all the functionality through to gson, but that also seems like a lot of work.

Is there any good way to achieve more friendly and maintainable serialization using vertx?

Expense answered 17/8, 2015 at 4:44 Comment(0)
A
46

I just submitted a patch to Vert.x that defines two new convenience functions for converting between JsonObject and Java object instances without the inefficiency of going through an intermediate JSON string representation. This will be in version 3.4.

// Create a JsonObject from the fields of a Java object.
// Faster than calling `new JsonObject(Json.encode(obj))`.
public static JsonObject mapFrom(Object obj)

// Instantiate a Java object from a JsonObject.
// Faster than calling `Json.decodeValue(Json.encode(jsonObject), type)`.
public <T> T mapTo(Class<T> type)

Internally this uses ObjectMapper#convertValue(...), see Tim Putnam's answer for caveats of this approach. The code is here.

Arable answered 2/2, 2017 at 13:52 Comment(1)
Good to know. Seems to be officially adopted at vertx.io/docs/apidocs/io/vertx/core/json/JsonObject.htmlSideshow
L
8

I believe Jackson's ObjectMapper.convertValue(..) functions don't convert via String, and Vert.x is using Jackson for managing JsonObject anyway.

JsonObject just has an underlying map representing the values, accessible via JsonObject.getMap(), and a Jackson serializer/deserializer on the public ObjectMapper instance in io.vertx.core.json.Json.

To switch between JsonObject and a data model expressed in Pojos serializable with Jackson, you can do:

JsonObject myVertxMsg = ... MyPojo pojo = Json.mapper.convertValue ( myVertxMsg.getMap(), MyPojo.class );

I would guess this is more efficient than going via a String (but its just a guess), and I hate the idea of altering the data class just to suit the environment, so it depends on the context - form vs performance.

To convert from Pojo to JsonObject, convert to a map with Jackson and then use the constructor on JsonObject:

JsonObject myobj = new JsonObject ( Json.mapper.convertValue ( pojo, Map.class ));

  • If you have implied nested JsonObjects or JsonArray objects in your definition, they will get instantiated as Maps and Lists by default. JsonObject will internally re-wrap these when you access fields specifying those types (e.g. with getJsonArray(..).

  • Because JsonObject is freeform and you're converting to a static type, you may get some unwanted UnrecognizedPropertyException to deal with. It may be useful to create your own ObjectMapper, add the vertx JsonObjectSerializer and JsonArraySerializer, and then make configuration changes to suit (such as DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES in Jackson).

Lovins answered 16/6, 2016 at 1:26 Comment(0)
R
7

Not sure if I've understood you correctly, but it sounds like you're trying to find a simple way of converting POJOs to JsonObject?

So, we have lots of pojos that we send over the EventBus as JsonObjects

I've found the easiest way is to use the vert.x Json class which has loads of helper methods to convert to / from Json Strings

JsonObject jsonObject = new JsonObject(Json.encode(myPojo));

Sometimes you need to add some custom (de)serializers, but we always stick with Jackson - that is what Vert.x is using so they work out of the box.

What we actually do, is provide an interface like the following:

public JsonObjectSerializable {
    public JsonObject toJson();
}

And all our pojos that need to be sent over the EventBus have to implement this interface.

Then our EventBus sending code looks something like (simplified):

public <T extends JsonObjectSerializable> Response<T> dispatch(T eventPayload);

Also, as we generally don't unit test Pojos, adding this interface encourages the developers to unit test their conversion.

Hope this helps,

Will

Reposition answered 19/8, 2015 at 9:11 Comment(6)
That is already what @Grdaneault suggested by saying '(vertx to load data -> json string -> gson to parse json into pojos -> make changes -> serialize to json string -> parse with vertx and save)' and you just replaced Gson with Jackson.Tinder
Yeah. I guess that seems fairly reasonable but it still feels dirty to convert to a string as an intermediary. I think for now the backing object is the way to go. I might be able to hook something up with reflection to automatically map the primitive fields at least. I just would rather not reinvent the wheel if there's something already availableExpense
@Grdaneault I understand. Although I'm curious why you don't like going via a String.Reposition
I'm sure the string creation and parsing algorithms have been optimized by their respective owners, but it still seems like it would introduce a lot of overhead to convert all the data to a string just to bring it back again. Although it looks like that might be the easiest wayExpense
This looks very inefficient: it serializes the myPojo object to JSON, then deserializes (when the JsonObject constructor is called), then re-serializes (once you serialize the JsonObject to send it over the wire).Arable
huh? How do we deserialize a string to a POJO?Louanne
C
2

Try this:

io.vertx.core.json.Json.mapper.convertValue(json.getMap(), cls)
Cayuse answered 3/1, 2017 at 11:31 Comment(0)
T
0

I think that using Gson as you described is the best possible solution at the current time.

While I agree that if a protocol layer was included in Vert.x it would indeed be first prize, using Gson keeps your server internals pretty organised and is unlikely to be the performance bottleneck.

When and only when this strategy becomes the performance bottleneck have you reached the point to engineer a better solution. Anything before that is premature optimisation.

My two cents.

Tractor answered 17/11, 2017 at 20:17 Comment(0)
U
-1

You can use the static function mapFrom:

JsonObject.mapFrom(object)
Underset answered 29/11, 2019 at 14:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.