Enterprise Java

Get to Know Customisation: JSON Binding Overview Series

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

Next article in this series covers how JSON-B handles custom object creation.

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. There is plenty more to know about the JSON Binding API than what I talk about in these blog posts.

Published on Java Code Geeks with permission by Alex Theedom, partner at our JCG program. See the original article here: Get to Know Customisation: JSON Binding Overview Series

Opinions expressed by Java Code Geeks contributors are their own.

Alex Theedom

Alex Theedom is a Senior Java Developer and has recently played a pivotal role in the architectural design and development of a microservice based, custom built lottery and instant win game platform. Alex has experience of Java web application development in a diverse range of fields including finance, e-learning, lottery and software development. He is the co-author of Professional Java EE Design Patterns and many articles.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button