You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
maybe I'm missing some important piece or my thinking is a bit too naïve, but I try to write a generic message passing library based on a json protocol.
packagecom.fasterxml.jackson.module.kotlin.test.githubimportcom.fasterxml.jackson.annotation.JsonTypeInfoimportcom.fasterxml.jackson.databind.DeserializationFeatureimportcom.fasterxml.jackson.databind.exc.MismatchedInputExceptionimportcom.fasterxml.jackson.module.kotlin.jacksonObjectMapperimportcom.fasterxml.jackson.module.kotlin.readValueimportjunit.framework.TestCase.assertEqualsimportorg.junit.Assert.assertThrowsimportkotlin.test.TestclassReproductionTest {
@JsonTypeInfo(use =JsonTypeInfo.Id.SIMPLE_NAME, include =JsonTypeInfo.As.PROPERTY, property ="@type")
sealedclassResponse<outErr, outB>{
data classSuccess<outB>(valresponse:B) : Response<Nothing, B>()
data classFailure<outErr>(valerror:Err) : Response<Err, Nothing>()
}
@JsonTypeInfo(use =JsonTypeInfo.Id.SIMPLE_NAME, include =JsonTypeInfo.As.PROPERTY, property ="@type")
sealedclassMessage {
data classText(valtext:String) : Message()
data classImage(valurl:String) : Message()
}
@JsonTypeInfo(use =JsonTypeInfo.Id.SIMPLE_NAME, include =JsonTypeInfo.As.PROPERTY, property ="@type")
sealedclassError {
data classNotFound(valmessage:String) : Error()
data classBadRequest(valmessage:String) : Error()
}
val asJson ="""{"@type":"Success","response":{"@type":"Text","text":"foo"}}"""val responseSuccess:Response<Error, Message> =Response.Success(Message.Text("foo"))
@Test
funtestReadAsValue() {
val mapper = jacksonObjectMapper()
val refType = mapper.typeFactory.constructParametricType(Response::class.java, Error::class.java, Message::class.java)
// Failing com.fasterxml.jackson.databind.exc.InvalidTypeIdException:// Could not resolve type id 'Success' as a subtype of ...
assertEquals(
responseSuccess,
mapper.readValue<Response<Error, Message>>(asJson, refType)
)
}
@Test
funtestWriteAsValue() {
val mapper = jacksonObjectMapper()
// Failing
assertEquals(
asJson,
mapper.writeValueAsString(responseSuccess) // actual: {"@type":"Success","response":{"text":"foo"}}
)
}
}
My expectations/goals would be:
if defined at the root type of Err/B type information should be included in the "response": {...} value and should be in control of the consuming code side (Message & Error would be defined there). But the type information is missing the the serialized value
and of course it should be deserializable
Is that possible to achieve? I tried various variations, including mapper.writerFor(refType), without any success.
Help is appreciated!
Greetings
The text was updated successfully, but these errors were encountered:
Your question
Hi,
maybe I'm missing some important piece or my thinking is a bit too naïve, but I try to write a generic message passing library based on a json protocol.
My expectations/goals would be:
"response": {...}
value and should be in control of the consuming code side (Message
&Error
would be defined there). But the type information is missing the the serialized valueIs that possible to achieve? I tried various variations, including
mapper.writerFor(refType)
, without any success.Help is appreciated!
Greetings
The text was updated successfully, but these errors were encountered: