Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Get to Know Customization: JSON Binding Overview Series

DZone's Guide to

Get to Know Customization: JSON Binding Overview Series

When using JSON-B in Java EE 8, you'll want to know about the annotation model used as well as how to configure it at runtime.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

Let’s take a look at how the annotation model and runtime configuration work when customizing the JSON Binding serialization and deserialization processes.

Annotation Method

Using the annotation method, it’s possible to customize the default serialization and deserialization behavior by annotating fields, JavaBean methods, and classes.

@JsonbNillable
@JsonbPropertyOrder(PropertyOrderStrategy.REVERSE)
public class Book {

    @JsonbProperty("cost")
    @JsonbNumberFormat("#0.00")
    private Float price;

}


For example, you could use the @JsonbNillable annotation to customize null handling and the @JsonbPropertyOrder annotation to customize the property order. These two annotations are specified at the class level.

You could specify the number format with the @JsonbNumberFormat annotation and change the name of a field with the @JsonbProperty annotation.

Runtime Configuration

Alternatively, you could choose to handle customization with the runtime configuration builder, by configuration an instance of JsonbConfig and passing it to the create method of the Jsonb builder, as shown in this code snippet.

JsonbConfig jsonbConfig = new JsonbConfig()
    .withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_DASHES)
    .withNullValues(true)
    .withFormatting(true);

Jsonb jsonb = JsonbBuilder.create(jsonbConfig);


Either way, the JSON Binding API provides extensive capabilities for the serialization and deserialization of Java objects. Let’s move on a look at how JSON-B handles custom object creation.

There is plenty more to know about the JSON Binding API than what I talk about in these blog posts and in my new book Java EE 8: Only What’s New, I cover this API in much more detail.

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:
java ,java ee 8 ,json-b ,annotations ,configuration ,runtime ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}