Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 352 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do you make Jackson use Kotlin default params for missing values?

#1
I've a Kotlin data class that looks like this,

data class SomeData(
private val notInJson: String = "some default value",
private val inJson: String
)

and a json string, I wish to deserialize into an instance of this class -

{
"inJson" : "value"
}

When I try to deserialize this data, I get an error that the parameter ```notInJson``` cannot be null. Seems like Jackson is passing a null value for it since it's missing in the json string.

Is there a way to have Jackson not pass in any value for it, so that the default specified in the class definition could be used?

--EDIT--

I do register a KotlinModule() with the mapper. Using version 2.7.8 or all jackson packages involved.

The following code -

@Test fun jackson_kotlin_module() {
val mapper = ObjectMapper().registerModule(KotlinModule())
println(
mapper.readValue("""{ "inJson" : "some value" }""", SomeData::class.java))
}

gives me -

com.fasterxml.jackson.databind.JsonMappingException: Instantiation of [simple
type, class SomeData] value failed (java.lang.IllegalArgumentException):
Parameter specified as non-null is null: method SomeData.<init>, parameter
notInJson
at [Source: { "inJson" : "some value" }; line: 1, column: 27]

Reply

#2
Default params is a Kotlin feature. Jackson does not know about it hence we need to inform it somehow to be able to support it. Thankfully there's the excellent [jackson-module-kotlin][1] which let's you do:

val mapper: ObjectMapper = ObjectMapper()
.registerModule(KotlinModule()) //let Jackson know about Kotlin
...
data class SomeData(
private val notInJson: String = "some default value",
private val inJson: String
)
...
val data = mapper.readValue<SomeData>("""{"inJson": "sample value"}""")
println(data) // -> SomeData(notInJson=some default value, inJson=sample value)


For instructions how to use it take a look at [jackson-module-kotlin Readme][1] and **pay special attention to versions** of the module and Jackson itself. In order for the default parameters to work you need [at least version `2.8.4`](

[To see links please register here]

) of [jackson-module-kotlin][1]


[1]:

[To see links please register here]

"jackson-module-kotlin"
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through