Is it possible to have two different values for th...
# serialization
k
Is it possible to have two different values for the
SerialName
of a polymorphic type? I basically want to migrate from one type to another whilst still supporting the old version
👀 1
a
For JSON there's
@JsonNames
https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/-json-names/ For non-JSON formats you can either write a custom Serializer (which can be a hassle). Or, define an internal constructor property for each alternative name, make each one nullable with
null
as the default value (so it's considered optional), mark each as internal, and define a public class property to expose whatever alternative isn't null using the elvis operator.
.
k
This sounds like a good solution for properties, but not for the value of the
type
field for polymorphic serialisation which is what I'm looking for.
Basically, I'd like a way to have something like
{ "type": "foo" }
and
{ "type": "bar" }
use the same type
a
Ah sorry, I misunderstood. You're right, I answered a different question! Are you working with JSON? If so, I'd look at using JsonContentPolymorphicSerializer https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/json.md#content-based-polymorphic-deserialization
r
Haven't tried it, but maybe?
Copy code
@SerialName("Foo")
data class Foo()

@SerialName("NewFoo")
typealias NewFoo = Foo