Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deserialize inherited Kotlin data classes with Gson

In an Android App I need to deserialize Json data for Kotlin data classes, with a single abstraction level. But I don't have any idea, to place correct properties in constructors.

As a simple version, let's say I have a Shape:

abstract class Shape(open val x: Int, open val y: Int)

with two derivations

data class Circle(val radius: Int, override val x: Int, override val y: Int): Shape(x, y)

and

data class Square(val width: Int, override val x: Int, override val y: Int): Shape(x, y)

So my target is, not to instantiate a Shape. So, instead always deserialize its derivations. Later I need to handle some collection properties in other classes like:

val shapes: List<Shape>

but I also have to know the derived type of each element.

When I try to deserialize the given example with Gson

val square = gson.fromJson(SQUARE_JSON, Square::class.java)

I always get an IllegalArgumentException

java.lang.IllegalArgumentException: class com.example.shapes.model.Square declares multiple JSON fields named x

at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:170)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
at com.google.gson.Gson.getAdapter(Gson.java:423)
at com.google.gson.Gson.fromJson(Gson.java:886)
at com.google.gson.Gson.fromJson(Gson.java:852)
at com.google.gson.Gson.fromJson(Gson.java:801)
at com.google.gson.Gson.fromJson(Gson.java:773)

Is there any possibility to deserialize this classes correctly without writing a custom Gson TypeAdapter or loosing data class advantages?

Thanks

like image 527
elcolto Avatar asked Apr 23 '17 20:04

elcolto


1 Answers

If you're not able to convert the base class to Kotlin just yet, the @Transient annotation worked for me.

data class Circle(val radius: Int, 
    @Transient val x: Int, 
    @Transient val y: Int): Shape(x, y)

data class Square(val width: Int, 
    @Transient val x: Int, 
    @Transient val y: Int): Shape(x, y)
like image 100
loeschg Avatar answered Sep 19 '22 00:09

loeschg