kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

Add Polymorphism architecture description for Protobuf into readme file

Open EMaksymenko opened this issue 4 years ago • 8 comments

There are missed information on how Protobuf plugin implements serializing sub-classes into byte array in the user manual. You have described ProtoNumber annotation usage but omit description of type field numbering and how to control it.

My use case is: I need to accept byte array on other external system (Kotlin incompatible) using generic Proto file declaration approach. I need to understand all aspects of field numbering to build similar Proto file with the same field numbers.

All custom fields can be annotated with ProtoNumber, but what about polymorphism type field?

EMaksymenko avatar Feb 15 '21 18:02 EMaksymenko

As I revers engineered polymorphic properties stored in wrappers with fields: string type = 1 bytes value = 2

EMaksymenko avatar Feb 15 '21 18:02 EMaksymenko

I've been following the discussion on this issue and was wondering if there has been any progress on adding the polymorphism docs for Protobuf. Additionally, I've noticed that the SerialName appears to be serialized as a string. Is this the intended behavior, and if so, could this be documented as well?

rotilho avatar Jan 07 '24 11:01 rotilho

Yes, the serial name is always a string.

sandwwraith avatar Jan 08 '24 11:01 sandwwraith

Are there any plans to add something like @ProtoSerialNumber?

rotilho avatar Jan 08 '24 14:01 rotilho

@rotilho There is @ProtoNumber for fields. Polymorphism, on the other hand, requires class (not field) serial name to deserialize a correct subclass.

sandwwraith avatar Jan 09 '24 11:01 sandwwraith

@rotilho For clarity. The "key"/type for polymorphic serialization is always the serial name of the instance type. If you put an @SerialName annotation on it, the value used is that of that annotation, otherwise it is the fully qualified class name. (And yes if you have duplicate names, that can confuse formats & polymorphic serialization).

pdvrieze avatar Jan 09 '24 14:01 pdvrieze

Thanks, @pdvrieze. I'm still not able to see the schema generated due to https://github.com/Kotlin/kotlinx.serialization/issues/2089, but this was my understanding based on the bytes generated (there are still a few bytes I didn't understand, though).

I have a very specific use case where thousands of very small messages will be exchanged per second. My initial expectation was that kotlinx-serialization would use the oneof and each type would have a different number, which is perfect for my use case. Encoding SerialName significantly increases the message size.

rotilho avatar Jan 10 '24 07:01 rotilho

Unfortunately the protobuf format doesn't have such special support for polymorphic types. It might be possible to create your own format variant that does, but you want to have stable ids (too easy to have automatic ids for types change).

As to the format, I would go for delegating to the protobuf format, but handling polymorphic children differently.

pdvrieze avatar Jan 10 '24 08:01 pdvrieze