hello! not specific to Kotlin but was wondering if...
# jackson-kotlin
d
hello! not specific to Kotlin but was wondering if anyone here knows what is the equivalent of
@JsonValue
for deserialization - e.g. I need to create wrapper around
String
value
Copy code
data class Example(@JsonValue val value: String)
above gets correctly serialized to just that actual
value
. Wondering if there is some handy annotation to deserialize it back without writing custom deserializer - or custom deserializer is the only way?
a
I thought if the constructor had just a single String parameter, Jackson would automatically deserialize it by constructing on the string - maybe the Kotlin module prevents that and enforces data objects should be JSON objects?
hm, yes, including the Kotlin module does prevent it from just working sadface
You can do this though, to which is at least a bit less painful that implementing a full custom deserializer:
Copy code
@JsonDeserialize(converter = Example.Converter::class)
    data class Example(@JsonValue val whatever: String) {
        class Converter : StdConverter<String, Example>() {
            override fun convert(value: String) = Example(value)
        }
    }
j
Copy code
data class Value @JsonCreator constructor (@get:JsonValue val value: String)
maybe?
a
unfortunately not. Ah, but making a JsonCreator method as a static method works:
Copy code
data class Example(@JsonValue val whatever: String) {
        companion object {
            @JvmStatic
            @JsonCreator
            fun create(str: String) = Example(whatever = str)
        }
    }
I thought I tried this before, I might have missed the
@JvmStatic
annotation
d
cool, thanks for the help! let me try it out
finally got a chance to test it
works like a charm
thanks!