Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of making Jackson behave more sensibly by default (also, more like Gson)
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
/**
* ObjectMapper customised for my tastes and most typical needs
*
* @author Joni Karppinen
*/
public class CustomObjectMapper extends ObjectMapper {
public CustomObjectMapper() {
// Deserialization
// Just ignore unknown fields, don't stop parsing
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// Trying to deserialize value into an enum, don't fail on unknown value, use null instead
configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
// Serialization
// Don't include properties with null value in JSON output
setSerializationInclusion(JsonInclude.Include.NON_NULL);
// Use default pretty printer
configure(SerializationFeature.INDENT_OUTPUT, true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment