Is it not possible to use `JsonObject` with protob...
# serialization
s
Is it not possible to use
JsonObject
with protobuf serialization?
java.lang.IllegalStateException: This serializer can be used only with Json format.Expected Encoder to be JsonEncoder, got class kotlinx.serialization.protobuf.internal.ProtobufEncoder (Kotlin reflection is not available)
h
No
👍🏻 1
Except if you write your own serializer
e
the protobuf standard itself doesn't have a natural way to represent JSON. you will have to design your own messages - there's types in
google.protobuf
that could mostly work but https://github.com/Kotlin/kotlinx.serialization/issues/653
j
Protobuf has a canonical JSON representation though
h
Yeah, but it's not (yet) supported by the kotlinx serialization library
p
In principle JsonObject could just be serialized as a map, and JsonArray as list. The JsonPrimitives as the relevant primitives. This is disabled as the serializers don't have the fallback for other formats enabled, and instead throw exceptions. Probably to avoid people relying on these when they shouldn't. Having said that, the alternative of just handling maps/arrays is almost equivalent (there is no common JsonElement that would allow this variation).
e
not quite that simple; protobuf tags are typed
p
@ephemient Of course, and it would not make for great protobuf, but the default mapping would be used that is also used for a map.
e
protobuf maps have a single value type. json objects do not
j
it'd be a json value which is a
oneof
of the five possible types
e
recursive, and that's what the "well-known" struct/list_value/value are. you could do that yourself too of course, but it's not built into kxs
j
We hit the same problem and ended up shipping hand-written serializers for the full `Struct`/`Value`/`ListValue` WKT stack — round-trips correctly against `protobuf-java`'s
JsonFormat.Printer
. If anyone wants to skip the boilerplate, it's part of ghost-serializer (
ghost-protobuf
module, on Maven Central).
e
translating github.com/protocolbuffers/protobuf/blob/…/struct.proto into Kotlin and mapping to/from kxs.json. totally untested but I don't see why it wouldn't work