https://kotlinlang.org logo
#jackson-kotlin
Title
# jackson-kotlin
p

phil-t

03/03/2022, 4:48 PM
👋 Hi all, in my automated tests I’m using Jackson to deserialise responses from two different GraphQL schemas into data classes, this works fine with a separate class for each schema but I’m trying to get it working with a single class to simplify the code. The responses from both schemas have a “data” node with different fields under that, so I’ve tried applying
@JsonAlias("data")
to two fields in my class so that I can have separate data classes representing the underlying data, but this doesn’t work - the alias seems to apply to one field or the other. I’ve tried these two methods so far but neither work:
Copy code
data class MyResponse(
    @JsonAlias("data")
    val dataOne: dataClassOne?,
    @JsonAlias("data")
    val dataTwo: dataClassTwo?
)

data class MyResponse(
    @JsonAlias("data")
    val dataOne: dataClassOne?, val dataTwo: dataClassTwo?
)
Is it possible to apply the alias to both fields so that the data will be deserialised to one or the other, or should I go back to using a separate class for each schema?
d

dinomite

03/03/2022, 8:27 PM
I think what you want to be using polymorphic deserialization https://medium.com/@david.truong510/jackson-polymorphic-deserialization-91426e39b96a
d

Dariusz Kuc

03/03/2022, 11:49 PM
You could potentially simplify your code by using a graphql client -> take a look at https://www.apollographql.com/docs/kotlin/ or https://opensource.expediagroup.com/graphql-kotlin/docs/client/client-overview which can handle serialization and deserializaton for you (latter supports Jackson and kotlinx serialization). *disclaimer: I am one of the maintainers of
graphql-kotlin
p

phil-t

03/04/2022, 9:11 AM
Thanks I’ll have a look at those 👍
10 Views