Defining profiles for dynamic ignore: @JsonView
Using Jackson JSON View to Protect Mass Assignment Vulnerabilities
Annotations
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
@JsonTypeName(SubType1.TYPE)
Customize objectMapper
Use jackson-datatype-jdk8 and jackson-datatype-jsr310 readValue
JavaType - preferred
- Use TypeFactory
Convert json to Map<String, SomeType>
Jersey -Customize Jackson objectmapper
@JsonCreator + @JsonValue
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
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.
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 TypesUse 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