Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Deserialization of JSON FIELD with Jackson in java?

Tags:

java

json

jackson

Given a simple entity class like this

public class User
{
  @JsonProperty
  public Calendar createdOn;

  @JsonProperty
  public String name;
}

Is there a way for me to hook into the jackson streaming API to custom deserialize ONLY the createdOn field? If there's not, then would something like this be possible in the future?

public class User
{
  @JsonProperty
  @JsonConverter(MyCustomCalendarConverter.class)
  public Calendar createdOn;

  @JsonProperty
  public String name;
}

It appears that I could custom deserialize the entire entity. I'm just curious if there is a way to customize the deserialization just a field at a time in order to, for example, custom parse a particular date format, or read an array of values into a custom entity, etc. while letting Jackson deserialize the rest of the entity normally.

like image 234
jandersen Avatar asked Mar 11 '11 06:03

jandersen


People also ask

How does Jackson deserialize dates from JSON?

This is how i'm deserializing the date: SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); getObjectMapper(). getDeserializationConfig(). setDateFormat(dateFormat);

Does Jackson use Java serialization?

This short tutorial shows how the Jackson library can be used to serialize Java object to XML and deserialize them back to objects.

How do I create a custom JSON deserializer?

The @JsonDeserialize annotation is used to declare custom deserializer while deserializing JSON to Java object. We can implement a custom deserializer by extending the StdDeserializer class with a generic type Employee and need to override the deserialize() method of StdDeserializer class.


1 Answers

You can define custom serialize a specific field using @JsonSerialize:

@JsonSerialize(using=MuCustomCalendarConverter.class)
like image 51
StaxMan Avatar answered Oct 21 '22 14:10

StaxMan