JSON-P provides the lower JSON node operations, JSON Binding API provides advanced operations on serialization of Java objects to JSON string and de-serialization of JSON string to Java objects.
An example to serialize a Java object to JSON string.
Person duke = new Person("Duke", LocalDate.of(1995, 5, 23));
duke.setPhoneNumbers(
Arrays.asList(
new PhoneNumber(HOME, "100000"),
new PhoneNumber(OFFICE, "200000")
)
);
Jsonb jsonMapper = JsonbBuilder.create();
String json = jsonMapper.toJson(duke);
LOG.log(Level.INFO, "converted json result: {0}", json);
JsonPath
allow you read the values of JSON nodes.
String name = JsonPath.parse(json).read("$.name");
assertEquals("Duke", name);
JsonPath.using()
method accepts custom configuration APIs to configure the json provider, eg. using Gson
instead of the default JSON provider.
Configuration config = Configuration.defaultConfiguration()
.jsonProvider(new GsonJsonProvider())
.mappingProvider(new GsonMappingProvider());
TypeRef<List<String>> typeRef = new TypeRef<List<String>>() {
};
List<String> numbers = JsonPath.using(config).parse(json).read("$.phoneNumbers[*].number", typeRef);
assertEquals(Arrays.asList("100000", "200000"), numbers);
Read the JSON string and map to an object directly.
Jsonb jsonMapper = JsonbBuilder.create();
Person person = jsonMapper.fromJson(JsonbTest.class.getResourceAsStream("/person.json"), Person.class);
assertEquals("Duke", person.getName());
Type type = new ArrayList<Person>() {}
.getClass()
.getGenericSuperclass();
List<Person> persons = jsonMapper.fromJson(JsonbTest.class.getResourceAsStream("/persons.json"), type);
assertTrue(persons.size() == 2);
JSON-B provides a series of annotations to adjust the serialization and deserialization for an object, including @JsonbProperty
, @JsonbPropertyOrder
, @JsonbTransient
etc.
@JsonbProperty
private String name;
Grab the source codes from my GitHub account, and have a try.