Given these two annotated classes :
public class Clazz1 {
@JsonbProperty("val11")
String value11;
@JsonbProperty("val12")
String value12;
.....
}
public class Clazz2 {
@JsonbProperty("val21")
String value21;
@JsonbProperty("val22")
String value22;
.....
}
What I need is to generate a JSON that includes two objects, for example:
Clazz1 c1 = new Clazz1();
Clazz1 c2 = new Clazz2();
c1.setValue1("ABC");
... other settings
JsonObject json = Json.createObjectBuilder()
.add("c1", c1)
.add("c2", c2)
.build();
The add method above doesn't accept objects, how to create a JSON that includes both objects c1 and c2 ?
One approach is to use the
JsonObjectBuilderAPI and build the object manually:Reference: https://docs.oracle.com/javaee/7/api/javax/json/JsonObjectBuilder.html
The other is to use the
JsonbBuilderAPI:Refernce: https://www.ibm.com/developerworks/library/j-javaee8-json-binding-3/index.html