https://kotlinlang.org logo
#serialization
Title
# serialization
a

akowal

08/15/2022, 12:23 PM
I have a class
data class(val foo: String, ...)
and in one context I need field
foo
to be serialized as
bar
(don’t need deserialization), but in other cases it should be de-/serialized as usual. How is it possible to achieve this?
p

Paul Woitaschek

08/15/2022, 4:56 PM
Create a KSerializer and use it depending on your context
a

akowal

08/17/2022, 7:39 AM
That won’t help. Serializers are protocol agnostic and only define how values are serialized, it doesn’t have any control over how fields are named in json.
p

Paul Woitaschek

08/17/2022, 7:51 AM
Ah you want to name it differently and not serialize it differently. Then go with a JsonTransformingSerializer