esensar / kotlinx-serialization-msgpack

MsgPack support for kotlinx.serialization -- msgpack.org[kotlinx.serialization]
https://www.ensarsarajcic.com/kotlinx-serialization-msgpack/
MIT License
44 stars 9 forks source link

Configuration option to encode enums as ordinal values #97

Closed krisbitney closed 1 year ago

krisbitney commented 1 year ago

Is your feature request related to a problem? Please describe. Some MessagePack libraries encode enum values as integers representing the ordinal value.

Describe the solution you'd like A configuration option to allow compatibility with MessagePack libraries that encode enum values as integers.

esensar commented 1 year ago

Hi!

Thank you for reporting this. There is no need to add a special configuration support for this. This is already supported by kotlinx.serialization with custom serializers.

Your enum needs to have a custom serializer provided in @Serializable(with = YourCustomSerializer::class) which would use ordinal instead of strings. You would have to implement YourCustomSerializer which would implement KSerializer interface.

Let me know if this works for you!

krisbitney commented 1 year ago

Hi!

Thank you for reporting this. There is no need to add a special configuration support for this. This is already supported by kotlinx.serialization with custom serializers.

Your enum needs to have a custom serializer provided in @Serializable(with = YourCustomSerializer::class) which would use ordinal instead of strings. You would have to implement YourCustomSerializer which would implement KSerializer interface.

Let me know if this works for you!

That is really inconvenient for my use case.

I work for https://polywrap.io/. We are creating a write-once-run-anywhere solution based on WebAssembly. We provide developer tools to facilitate building plugins for developer tools in various languages. Our tools work by serializing and deserializing data in MessagePack format to cross language barriers. The developer tools use codegen to generate boilerplate code that makes the plugins easy to write, and part of the boilerplate is serializable classes.

It would really be much simpler just to support this common MessagePack format directly than to write codegen logic to generate custom serializers for every enum.

I already wrote the feature and opened a PR https://github.com/esensar/kotlinx-serialization-msgpack/pull/98.

What do you view as the downside of this?

esensar commented 1 year ago

The only downside is the additional maintenance, but it is a really small feature, so I guess it is fine.

krisbitney commented 1 year ago

The only downside is the additional maintenance, but it is a really small feature, so I guess it is fine.

Thank you, I really appreciate it. 🙏