VOOZH about

URL: https://www.javacodegeeks.com/2018/04/get-to-know-customisation-json-binding-overview-series.html

⇱ Get to Know Customisation: JSON Binding Overview Series - Java Code Geeks


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.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

Tags
JSON
πŸ‘ Photo of Alex Theedom
Alex Theedom
April 24th, 2018Last Updated: April 24th, 2018
0 84 1 minute read

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

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

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz