6

I have the following Kotlin data class:

data class TestObject(
        val boolField: Boolean,
        val stringField: String,
        val nullBoolField: Boolean = true,
        val nullStringField: String = "default",
        val notThereBoolField: Boolean = true,
        val notThereStringField: String = "not there"
)

I am then attempting to deserialize some JSON into this class using Jackson v2.9.4 with the Jackson Kotlin plugin v2.9.4.1. The test JSON is as follows:

{
    "boolField": true,
    "stringField": "string",
    "nullBoolField": null,
    "nullStringField": null
}

The first two and the last two fields deserialize successfully - with the values from the JSON and the default values from the Kotlin data class, as appropriate. However, when the middle pair of fields are present, the deserialization fails with:

Instantiation of [simple type, class com.example.TestObject] value failed for JSON property nullStringField due to missing (therefore NULL) value for creator parameter nullStringField which is a non-nullable type

I know I could solve the problem by changing the types of nullBoolField and nullStringField to Boolean? and String? respectively, but since default values are known I would rather not have to push the handling of null further down into the code by doing that.

The question is: is there any way of configuring Jackson to use the Kotlin data class default values for non-nullable properties when the property is present in the JSON but set to null?

pwolaq
  • 6,343
  • 19
  • 45
George
  • 497
  • 6
  • 11
  • Did you find any answer on it, I still have the same issue, and i can't resolve it at the moment...? – Rémi P Mar 07 '18 at 14:24

3 Answers3

1

You could try first to filter null values from json and after to deserialize it to Kotlin object.

Or you may to try add feature to kotlin-jackson module, with adding a new feature parameter, which will enable a null ignoring from json parameters and use default values. You may do this by modify this line (if I'm not mistaken)

kurt
  • 1,510
  • 2
  • 13
  • 23
  • `you may to try add feature to kotlin-jackson module, with adding a new feature parameter, which will enable a null ignoring from json parameters and use default values`. How would that work? Any examples? – denvercoder9 Aug 18 '19 at 06:40
0

If you don't have any value and field is non-nullable then you should not pass it in request body:

{
    "boolField": true,
    "stringField": "string"
}

This results in following object, as expected:

{
    "boolField": true,
    "stringField": "string",
    "nullBoolField": true,
    "nullStringField": "default",
    "notThereBoolField": true,
    "notThereStringField": "not there"
}
pwolaq
  • 6,343
  • 19
  • 45
  • That would be the easy way out I agree. However, in this case, I don't control the source JSON so am trying to find a way to get the intended result when deserializing. – George Feb 12 '18 at 20:21
  • @George in such situation you could use a DTO and then create TestObject based on its properties – pwolaq Feb 12 '18 at 20:42
0

If you're mapping to Java 8, you can use:

    val kotlinModule = KotlinModule.Builder()
        .disable(KotlinFeature.StrictNullChecks)
        .build()
    val objectMapper = ObjectMapper()
    objectMapper.registerModules(
        Jdk8Module(),
        kotlinModule
    )
N Sarj
  • 374
  • 3
  • 11