@JsonSerialize

From NovaOrdis Knowledge Base
Revision as of 18:11, 14 November 2018 by Ovidiu (talk | contribs) (→‎Example)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

External

Internal

Overview

The annotation allows to control serialization to JSON on a field-by-field basis. The fields need to be annotated as follows:

@JsonSerialize(using = MyDateSerializer.class)
private Date timestamp;

The serializer class should be provided:

public class MyDateSerializer<Date> extends JsonSerializer<Date> {
  @Override
  public void serialize(Date value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
    DateFormat f = new SimpleDateFormat("MM/dd");
    String s = f.format(value); 
    gen.writeString(s);
  }
}

Example

@JsonSerialize Playground Example