Jackson Essentials


Defining profiles for dynamic ignore: @JsonView
Using Jackson JSON View to Protect Mass Assignment Vulnerabilities
@JsonView(Views.Public.class)
@JsonView(Views.Internal.class)

Annotations
@JsonInclude(Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonIgnoreProperties({"propertyA", "propertyB"})
@JsonIgnoreType

@JsonProperty("anotherName") - give a different name
@JsonIgnore

Custom JSON Deserialization and Serialization 
@JsonDeserialize, @JsonSerialize
JsonDeserializer, JsonSerializer 

@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE) 

@JsonUnwrapped
@JsonUnwrapped public Location location; 

Polymorphic Types
@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.PROPERTY,
    property = "type")
@JsonSubTypes({
    @Type(value = A.class, name = "a"),
    @Type(value = B.class, name = "b") })

@JsonTypeName(SubType1.TYPE)

Customize objectMapper
Jackson Date Serialize + Deserialize
We can configure all sub classes of ConfigFeature: MapperFeature, DeserializationFeature, SerializationFeature, JaxRSFeature.

new ObjectMapper()
        .registerModule(new JavaTimeModule())
        .setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"))
        .configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true)
        .configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true)
        .configure(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS, true)
        .configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false)
        .configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false)
        .configure(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS, false)
        .configure(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY, false)
        .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
        .configure(DeserializationFeature.FAIL_ON_UNRESOLVED_OBJECT_IDS, false)
        .setSerializationInclusion(Include.NON_NULL)
        .configure(SerializationFeature.INDENT_OUTPUT, true);
Using Java8 Types
Use jackson-datatype-jdk8 and jackson-datatype-jsr310 readValue
JavaType - preferred
- Use TypeFactory
JavaType javaType = mapper.getTypeFactory().constructCollectionType(List.class, SomeType.class);
TypeFactory.defaultInstance().constructType(SomeType.class);
TypeFactory.defaultInstance().constructMapType(Map.class, String.class, SomeType.class);

objectMapper.readValue(json, JavaType)

SomeType[] array = mapper.readValue(jsonArray, SomeType[].class);
TypeReference
Convert json to Map<String, SomeType>
mapper.readValue(json, new TypeReference<Map<String, SomeType>>(){});

Jersey -Customize Jackson objectmapper
@Provider
public class JerseyObjectMapperProvider implements ContextResolver<ObjectMapper> {
    private static ObjectMapper objectMapper = Util.createFailSafeObjectmapper();
    @Override
    public ObjectMapper getContext(final Class<?> type) {
        return objectMapper;
    }
}

@JsonCreator + @JsonValue
public enum XEnum {
  A("value1"), B("value2");
  private final String value;
  XEnum(final String value) {
   this.value = value;
  } 
  @JsonValue
  String getValue() { return value;}
  public static XEnum fromString(final String value) {
   return getEnumfromString(value, XEnum)
  }
}

public static <E extends Enum<E>> E getEnumfromString(final String value, final Class<E> enumType) {
  return Enum.valueOf(enumType, value.toUpperCase());
}

To use scala case class with Jackson: add @BeanProperty to the field.

Read More
Jackson Essentials - the JSON Libaray
Using Jackson JSON View to Protect Mass Assignment Vulnerabilities
Merge JSON Objects: Jackson + BeanUtils.copyProperties
Jackson Generic Type + Java Type Erasure
Jackson Date Serialize + Deserialize

Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)