I am getting trouble with parsing JSON with Jackson and I want to ignore null properties. Here is my code.
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
data class ParsedSurvey(
val items: List<ParsedSurveyItem> = listOf()
)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
data class ParsedSurveyItem(
val type: String = "",
val text: String = "",
val instructions: String = "",
val prog: String = "",
val `var`: String = "",
val columns: List<ParsedSurveyAnswer> = listOf(),
val rows: List<ParsedSurveyAnswer> = listOf(),
val num: String = "",
val multi: Boolean = false,
val random: Boolean = false,
val min: Int = -1,
val max: Int = -1,
val recordOrder: Boolean = false,
val rowLength: Int = -1
)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
data class ParsedSurveyAnswer @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) constructor(
val text: String = "",
val prog: String = "<p></p>",
@JsonProperty("isOpen") val isOpen: Boolean = false
)
If I try to set rows property in ParsedSurveyItem to null. I am getting this error:
value failed for JSON property rows due to missing (therefore NULL) value for creator parameter rows which is a non-nullable type.
Jackson doesn't ignore
Why Jackson parses null values? Thanks for help.
You could set the rows property null
only if its nullable. It means
change it to
val rows: List<ParsedSurveyAnswer>?
You could also remove listOf()
& @JsonIgnoreProperties(ignoreUnknown = true)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With