Is or will it be possible to write a serializer fo...
# serialization
p
Is or will it be possible to write a serializer for
Map<String, (Number|Boolean|String)?>
, ideally in a serialization format agnostic way? I guess what I'm asking for is
Decoder.peekType()
. I've looked at
JsonDecoder.decodeJsonElement()
but the implementation is all
internal
.
e
n
do you want union type in the serialized thing ? or just support either one of them with a single serializer ?
p
@ephemient That's my current workaround, but I'd rather not convert the input to JsonElement (in fact I need to support nested lists and maps too) and I want this to be serialization format agnostic.
e
JsonPrinitive there will serialize like any other sealed class
p
@Nikky I don't understand your question
@ephemient I want to directly serialize a
Map<String, (Number|Boolean|String|List|Map)?>
without any input conversion steps.
e
not all formats will support variant types so if you want to be format agnostic you need a custom wrapper somewhere
p
I think all I'm missing is
Decoder.peekType()
, a generalization of what
JsonDecoder.decodeJsonElement
already does.
e
and not all formats will support that
p
@ephemient I'm OK with doing this for all formats that support it.
(in a format agnostic way)
e
well that's basically just JSON
p
you sure? I don't think that's correct
and having this just for JSON would be much better than nothing
e
properties format doesn't have types, relies on serialized type to know what the type of. protobuf schema doesn't support variant types. might work with cbor, not sure.
to make it work work json just cast the decoder
p
Yes, it does work for CBOR.
JsonDecoder
returns
JsonElement
, which requires me to recursively convert back to
Map<String, (Number|Boolean|String|List|Map)?>
, which is my current workaround.
What would also help is an easy way to write a serializer that uses its own type tags.
n
it sounds like what you want is to make the serializer of the value a argument to your
Serializer
so you can construct it the same way as
MapSerializer(keySerializer, valueSerializer)
or
ListSerializer(valueSerializer)
unless the type of your values in the map/object is mixed
p
yes it's mixed, that's the point
Basically I need to serialize
Map<String, Any>
. If necessary, I can tag values in the serialized format, but it's not clear to me how to make this work without using wrapper types similar to
JsonElement
on the Kotlin side (which I absolutely don't want), and it shouldn't be necessary as long as I'm using JSON/CBOR.
e
just make a custom serializer that wraps/unwraps, see the doc I linked earlier
p
I don't want to wrap on the Kotlin side because recursively wrapping/unwrapping a large data structure is inefficient. Even with internal wrapping, I'm not sure if I can have a
KSerializer<Map<String, Any>>
(last time I tried I got some compiler plugin error).