Skip to content

Instantly share code, notes, and snippets.

@jonikarppinen
Last active June 28, 2018 21:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonikarppinen/9b7f3872257bce27f8e2 to your computer and use it in GitHub Desktop.
Save jonikarppinen/9b7f3872257bce27f8e2 to your computer and use it in GitHub Desktop.
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