Close

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

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

In the last tutorial we saw how to use @JsonSerialize#converter and @JsonDeserialize#converter attributes. In this tutorial we will use @JsonDeserialize#using and @JsonDeserialize#using which require JsonSerializer and JsonDeserializer class types.

Example

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(using = LocalDateTimeSerializer.class)
  @JsonDeserialize(using = LocalDatetimeDeserializer.class)
  private LocalDateTime lastUpdated;
    .............
}

Serializer

package com.logicbig.example;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.format.FormatStyle;

public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
  static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);

  @Override
  public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider provider)
          throws IOException {
      try {
          String s = value.format(DATE_FORMATTER);
          gen.writeString(s);
      } catch (DateTimeParseException e) {
          System.err.println(e);
          gen.writeString("");
      }
  }
}

Deserializer

package com.logicbig.example;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;

public class LocalDatetimeDeserializer extends JsonDeserializer<LocalDateTime> {

  @Override
  public LocalDateTime deserialize(JsonParser p, DeserializationContext ctx)
          throws IOException {
      String str = p.getText();
      try {
          return LocalDateTime.parse(str, LocalDateTimeSerializer.DATE_FORMATTER);
      } catch (DateTimeParseException e) {
          System.err.println(e);
          return null;
      }
  }
}

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-01T16:02:40.887586300}
JSON string: {"pair":"USD/JPY","rate":109.15,"lastUpdated":"Jun 1, 2018, 4:02:40 PM"}
-- JSON to Java object --
Java Object: CurrencyRate{pair='USD/JPY', rate=109.15, lastUpdated=2018-06-01T16:02:40}

Registering custom serializer and deserializer with ObjectMapper

Instead of using @JsonDeserialize and @JsonSerialize, we can register our custom serializer and deserializer on ObjectMapper level, check out this tutorial.

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

JsonSerializer and JsonDeserializer Example Select All Download
  • jackson-serialize-deserialize-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • CurrencyRate.java

    See Also