Close

Jackson JSON - Using @JsonSerialize and @JsonDeserialize with Converter for custom conversion

[Updated: Jun 1, 2018, Created: Jun 1, 2018]

In this example we will learn how to do custom deserialization and serialization using @JsonSerialize and @JsonDeserialize annotations.

We will use @JsonSerialize#converter and @JsonDeserialize#converter attributes. These attributes require com.fasterxml.jackson.databind.util.Converter class type. We will, of course, also need to provide the custom converter implementations.

Example

In this example we are going to convert LocalDateTime to a formatted string and vice-versa.

Java Object

package com.logicbig.example;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import java.time.LocalDateTime;

public class CurrencyRate {
  private String pair;
  private double rate;
  @JsonSerialize(converter = LocalDateTimeToStringConverter.class)
  @JsonDeserialize(converter = StringToLocalDatetimeConverter.class)
  private LocalDateTime lastUpdated;
    .............
}

Custom Converters

package com.logicbig.example;

import com.fasterxml.jackson.databind.util.StdConverter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class LocalDateTimeToStringConverter extends StdConverter<LocalDateTime, String> {
  static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);

  @Override
  public String convert(LocalDateTime value) {
      return value.format(DATE_FORMATTER);
  }
}
package com.logicbig.example;

import com.fasterxml.jackson.databind.util.StdConverter;
import java.time.LocalDateTime;

public class StringToLocalDatetimeConverter extends StdConverter<String, LocalDateTime> {

  @Override
  public LocalDateTime convert(String value) {
      return LocalDateTime.parse(value, LocalDateTimeToStringConverter.DATE_FORMATTER);
  }
}

Main class

public class ExampleMain {
  public static void main(String[] args) throws IOException {
      System.out.println("-- Java object to JSON --");
      CurrencyRate cr = new CurrencyRate();
      cr.setPair("USD/JPY");
      cr.setRate(109.15);
      cr.setLastUpdated(LocalDateTime.now());
      System.out.println("Java object: " + cr);

      ObjectMapper om = new ObjectMapper();
      String s2 = om.writeValueAsString(cr);
      System.out.println("JSON string: "+s2);

      System.out.println("-- JSON to Java object --");
      CurrencyRate cr2 = om.readValue(s2, CurrencyRate.class);
      System.out.println("Java Object: "+cr2);
  }
}
-- Java object to JSON --
Java object: CurrencyRate{pair='USD/JPY', rate=109.15, lastUpdated=2018-06-01T15:03:01.596670}
JSON string: {"pair":"USD/JPY","rate":109.15,"lastUpdated":"Jun 1, 2018, 3:03:01 PM"}
-- JSON to Java object --
Java Object: CurrencyRate{pair='USD/JPY', rate=109.15, lastUpdated=2018-06-01T15:03:01}

Example Project

Dependencies and Technologies Used:

  • jackson-databind 2.9.5: General data-binding functionality for Jackson: works on core streaming API.
  • JDK 10
  • Maven 3.3.9

@JsonSerialize and @JsonDeserialize with Converter Example Select All Download
  • jackson-serialize-deserialize-converter
    • src
      • main
        • java
          • com
            • logicbig
              • example

See Also