Kotlin / kotlinx.serialization

Kotlin multiplatform / multi-format serialization
Apache License 2.0
5.44k stars 623 forks source link

Provide a best practice for formats that requiring a specified key order. #2646

Closed iseki0 closed 7 months ago

iseki0 commented 7 months ago

What is your use-case and why do you need this feature? I'm trying to implement an encoder/decoder for the Bencoding format. This format requires that keys in the dictionary type appear in sorted order. Currently, the structure encoding process is completely controlled by the serializer, which does not have a method to determine the best encoding order.

Describe the solution you'd like Provide a way for the serializer to know the best order for encoding.

pdvrieze commented 7 months ago

@iseki0 Changing the way serializers work is a very complex undertaking that would require a lot of time at least for compatibility.

In the meantime the way to solve it is to "memoize" the serialization requests. Have the CompositeEncoder contain an array of serialization functions that you set when serialization of each member is requested. Then in the endStructure you actually perform the member serializations in the desired order. (Xml has a similar issue due to attributes and elements needing to be handled, and now actually having a feature allow almost arbitrary reordering)

iseki0 commented 7 months ago

@iseki0 Changing the way serializers work is a very complex undertaking that would require a lot of time at least for compatibility.

In the meantime the way to solve it is to "memoize" the serialization requests. Have the CompositeEncoder contain an array of serialization functions that you set when serialization of each member is requested. Then in the endStructure you actually perform the member serializations in the desired order. (Xml has a similar issue due to attributes and elements needing to be handled, and now actually having a feature allow almost arbitrary reordering)

Well. Maybe it's the easiest way for a workaround, since many bencoding data is not so large.

sandwwraith commented 7 months ago

It is easy to get a sorted order from program one, but almost impossible to get program from sorted. Therefore, for serializers to be independent from formats, they always emit properties in program order. In case you need a sorted order, I recommend following @pdvrieze's advice: store properties in intermediate structure and sort them in endStructure. We do not plan to provide customizations to KSerializer's generation, because it may affect all formats, sometimes in unpredictable ways.