Re: Partial JSON deserialization

Richard Evans
 

I thought of both of these - JsonObject would work but then I need to get the fields out manually.  For the adapter - what would I convert from?  If the shape of the object determined by the type, I don't have access to that in the adapter.  And Json adapters convert from one deserialized type to another - for example string to date, so don't see how that helps.

I really need to do the deserialization in two steps - one of the outer object to give the type and another for the inner object.

Richard

-------- Original Message --------
you could accomplish this by defining your "data" field type as type javax.json.JsonObject so you can work with it raw:

public class Foo {
  public String type;
  public JsonObject data;
}

A more elegant solution would be to write a custom javax.json.bind.adapter.JsonbAdapter and reference it using @JsonbTypeAdapter on your data model class, for example:

public class Foo {
  @JsonbTypeAdapter(FooDataAdapter.class)
  public FooData data;
}

The advantage of this approach would be that you can implement your own adapt{To,From}Json() methods where you could inspect the data to determine the concrete type in the case FooData has multiple subclasses (I'm guessing this is what you're trying to achieve by encoding the type information in the JSON)

--
Oracle
Richard Evans | Product Architect
Phone: +44 1223 228408 | Mobile: +44 7973 839664
Oracle Product Development
Newton House Cambridge Business Park | Cambridge | CB4 0WZ | United Kingdom


ORACLE Corporation UK Ltd is a company incorporated in England & Wales | Company Reg. No. 1782505 | Reg. office: Oracle Parkway, Thames Valley Park, Reading RG6 1RA
Green Oracle Oracle is committed to developing practices and products that help protect the environment

Join jsonb-spec@javaee.groups.io to automatically receive all group messages.