Closed leostera closed 8 months ago
I think unordered should be the default, and forcing ordered should be the one with an override in the ppx. Do you think it should be the other way around?
I can probably work on this if you want btw, I literally cannot proceed without this it will be too painful to do twitch things without this haha
@tjdevries i've implemented some support for this, let me know if it solves your use-case.
At the moment when we try to deserialize a record we expect fields to appear in a specific order.
So if your record is:
Then the
deserialize_user
function (handwritten or derived) will only manage to deserialize data that is in this exact order:This is perfectly fine for many formats, so we can't enforce arbitrary order of keys in records, but we want to support serializing and deserializing maps.
Maps are different from records in that they would allow us to do this. Maps here are also not referring to OCaml
Map
module. This is just a name we are using for unordered collections of key-value pairs.Implementation Notes
I'd like to see a
serialize_map
anddeserialize_map
pair of functions added to theSerializer
andDeserializer
modules, with their corresponding helper functions for handwriting de/serializers and an API that is as close as possible to records.This would allow the formats to decide whether they want to serialize fields in an ordered or an unordered way.
For serialization we can probably get something that is the exact same:
For deserialization we have to do some work, since the keys may come in any order. Here's how I'd expect the API to look like: