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.
Serialization
An example to serialize a Java object to JSON string.
Person duke =newPerson("Duke",LocalDate.of(1995,5,23));duke.setPhoneNumbers(Arrays.asList(newPhoneNumber(HOME,"100000"),newPhoneNumber(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.
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 =newArrayList<Person>() {}.getClass().getGenericSuperclass();List<Person> persons =jsonMapper.fromJson(JsonbTest.class.getResourceAsStream("/persons.json"), type);assertTrue(persons.size() ==2);
JSON-B annotations
JSON-B provides a series of annotations to adjust the serialization and deserialization for an object, including @JsonbProperty, @JsonbPropertyOrder, @JsonbTransient etc.
@JsonbPropertyprivateString name;
Grab the source codes from my GitHub account, and have a try.