Java EE 8 By Example
  • Introduction
  • Overview
    • Example Codes
  • JSF 2.3
    • Activating CDI in JSF 2.3
    • Run applications in JSF 2.2 compatible mode
    • CDI alignment
    • CDI compatible @ManagedProperty
    • Inject support in Converter, Validator and Behavor
    • Websocket support
    • UIData improvements
    • Class level bean validation with f:valdiateWholeBean
    • Java 8 DateTime support
    • PostRenderViewEvent: publising events after view is rendered
    • Search expression framework
  • CDI 2.0
    • Java SE support
    • Event Priority
    • Async Events
    • Register Beans dynamicially
    • Configurators and Intercept Producers
  • JPA 2.2
    • Java 8 Datetime support
    • Return Stream based result from Query
    • More CDI Alignments
  • JSON-B 1.0
  • JSON-P 1.1
  • Bean Validation 2.0
  • JAXRS 2.1
    • Async improvements
    • Server Sent Event
    • Reactive Client
  • Java EE Security API 1.0
    • HttpAuthenticationMechanism
    • IdentityStore
    • SecurityContext
  • Servlet 4.0
    • Server Push
    • Runtime Discovery of Servlet Mappings
    • Http Trailer
  • MVC 1.0
    • Getting started with MVC
    • Handling form submission
    • Exception handling and form validation
    • Processing PUT and DELETE methods
    • Page navigation
    • MVC and CDI
    • Security
    • Bean parameter conversion
    • View engine
Powered by GitBook
On this page
  • Serialization
  • Deserialization
  • JSON-B annotations

Was this helpful?

JSON-B 1.0

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 = 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);

Deserialization

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 annotations

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;
PreviousMore CDI AlignmentsNextJSON-P 1.1

Last updated 4 years ago

Was this helpful?

Grab the from my GitHub account, and have a try.

source codes