Stephan Schroeder
11/12/2019, 3:27 PMData.Single
but it fails.
sealed class Data<T: Any> {
class Single<T: Any>(val single: T?): Data<T>()
class ListOf<T: Any>(val list: List<T>): Data<T>()
}
Could the reason for the mismatch be that the constructor parameter single
is nullable??
The error message is: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of uk.co.whichdigital.pricingengine.Data$Single
(although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value (‘up and running’)
Here is my current jsonMapper config:
private val jsonMapper = ObjectMapper().apply {
registerModule(KotlinModule())
configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true)
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
}
UPDATE: I changed the code to `Data.Single`’s generic parameter is nullable class Single<T>(val single: T): Data<T>()
but the error message didn’t change.diesieben07
11/12/2019, 3:28 PMSingle
would be a JSON object:
{
"single": <value here>
}
@JsonCreator
on the constructor and @JsonValue
on the property.Stephan Schroeder
11/12/2019, 3:33 PMclass Single<T>(val single: T): Data<T>()
I guess the constructor is implicit and so annotated version would be class Single<T>(@JsonValue val single: T): Data<T>()
!?
The error message didn’t change. (no String-argument constructor/factory method to deserialize)diesieben07
11/12/2019, 3:35 PMclass Single<T> @JsonCreator constructor(@JsonValue val single: T) : Data<T>()
Stephan Schroeder
11/12/2019, 3:37 PMdiesieben07
11/12/2019, 3:39 PMStephan Schroeder
11/12/2019, 3:49 PMInvalidDefinitionException: Invalid type definition for type `uk.co.whichdigital.pricingengine.GraphQlResponse`: Argument #0 has no property name, is not Injectable: can not use as Creator [constructor for uk.co.whichdigital.pricingengine.GraphQlResponse, annotations: {interface com.fasterxml.jackson.annotation.JsonCreator=@com.fasterxml.jackson.annotation.JsonCreator(mode=DEFAULT)}]
with
data class GraphQlResponse<T, D: Data<T>>@JsonCreator constructor(
@JsonValue val data: Map<String, D>?,
@JsonValue val errors: List<GraphQlError>?
)
diesieben07
11/12/2019, 3:49 PMStephan Schroeder
11/12/2019, 3:50 PMdiesieben07
11/12/2019, 3:56 PMclass SingleDeserializer : StdConverter<String, Data.Single<String>>() {
override fun convert(value: String?): Data.Single<String> {
return Data.Single(value)
}
}
@JsonDeserialize(converter = SingleDeserializer::class)
class Single<T: Any>(@JsonValue val single: T?): Data<T>()
Stephan Schroeder
11/12/2019, 3:56 PMdata class
was fine. I had to add @JsonProperty
to the fields 😮 @JsonProperty("data") val data: Map<String, D>?