Zakhar
08/11/2023, 9:50 AM{
"cars": []
}
Let’s name it just Base.
Next i have a base class for cars. Just Car class. And two subclasses. Truck and Tesla.
@Serializable
open class Car {
@SerialName("ms")
protected val maxSpeed: Int = 0
}
@Serializable
class Truck(
@SerialName("lc")
private val loadCapacity: Int = 0,
@SerialName("ft")
private val fuelType: String = ""
): Car()
@Serializable
class Tesla(
@SerialName("rt")
private val rechargeTime: Int = 0
): Car()
As you see these two subclasses have different properties.
Let’s add some cars to the array of the base class:
{
"cars": [{ ms: 80, lc: 1000, ft: "diesel" }, { ms: 180, rt: 4 }]
}
And now i want to transform this json object into Base class.
class Base {
val cars = listOf<Car>()
}
I know there is a polymorphic serializers module that allows me to register a hierarchy of classes.
Maybe i need to do something like this:
Register a hierarchy of classes.
val json = Json {
serializersModule = SerializersModule {
polymorphic(Car::class) {
subclass(Truck::class)
subclass(Tesla::class)
}
}
}
Add an information about types into the json
{
"cars": [{ type: "truck", ms: 80, lc: 1000, ft: "diesel" }, { type: "tesla", ms: 180, rt: 4 }]
}
@Serializable
@SerialName("truck")
class Truck(
@SerialName("lc")
private val loadCapacity: Int = 0,
@SerialName("ft")
private val fuelType: String = ""
): Car()
@Serializable
@SerialName("tesla")
class Tesla(
@SerialName("rt")
private val rechargeTime: Int = 0
): Car()
And then just transform a string into the base class.
json.decodeFromString<Base>(string)
But this is not working. I see that Base class is not in the hierarchy so it can not be achieved by this way.Adam S
08/11/2023, 9:59 AMBut this is not workingCan you say more about what's not working? Is there an error, or does the decoded data not match in some way?
andylamax
08/11/2023, 10:28 AM@Serializable
Zakhar
08/11/2023, 11:23 AMCar
class is not abstract, it is open.
Subclasses and the Car
class already marked as @Serializable
The error i am getting is
Unexpected JSON token at offset 11: Encountered an unknown key 'type' at path
Zakhar
08/11/2023, 11:30 AMCar
class is abstract there is no problems. Everything works fine. But in my case the class is open.
My question is only about how to achieve this with a base class marked as open.Dominaezzz
08/11/2023, 11:40 AMCar
class is the issue. It needs to be abstract and maxSpeed
needs to be abstractglureau
08/11/2023, 12:13 PMCarInterface
, Car will inherit this interface, and then your configuration becomes:
val json = Json {
serializersModule = SerializersModule {
polymorphic(CarInterface::class) {
subclass(Truck::class)
subclass(Car::class)
subclass(Tesla::class)
}
}
}
Because if Car is open, it means it can be instantiated, so it's another possibility of the polymorphism (even if the naming is questionnable in this case).