Last active
June 28, 2018 21:28
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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