A JsonObject can be transformed into it's corresponding class instance via:
Pojo pojo = JsonbBuilder.create().fromJson(jsonObject.toString(), Pojo.class)
However, it seems to be inefficient to use jsonObject.toString()
as a String is an other intermediate representation, that contains the same information. I need to transform the jsonObject before the object binding. So is there a more efficient way to achieve the binding from a JsonObject?
(Note I want to implement with Java EE 8 standards, so Gson and Jackson is not an option, but may be concepts of it). There is currently not answer in the Yasson group so hopefully, someone finds this. Michael Schnell also proposed a JsonStructure binding, but there is no solution yet too.
It is not possible with the JSON-B 1.0 standard to convert JSON-P object <--> POJO. However, enough people have asked for it that it's certainly something we will consider for the next version of JSON-B.
In the meantime, converting to String or using a custom adapter is your best option.
If you are concerned about performance with JSON-B, the #1 thing you can do to improve JSON-B performance is cache the instance of Jsonb
, namely, don't call JsonbBuilder.create()
each time you need to call to/fromJson, because all of the annotation scanning and class parsing happens upon creation of the Jsonb
instance.
Update (June 2019):
Although there is still no JSON-B standard way of doing this, the JSON-B reference implementation, Eclipse Yasson, has added support for this on the org.eclipse.yasson.YassonJsonb
interface in this PR. Hopefully this function will get included in the next version of the JSON-B spec.