Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin - Jackson ignore null values

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.

like image 552
Václav Pavlíček Avatar asked Aug 03 '17 08:08

Václav Pavlíček


1 Answers

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)

like image 177
praveenK Avatar answered Sep 28 '22 05:09

praveenK