xyncro / chiron

JSON for F#
https://xyncro.tech/chiron
MIT License
173 stars 41 forks source link

Object serialization: do not use a `Map<string, Json>` #90

Open toburger opened 6 years ago

toburger commented 6 years ago

I would suggest to use a list<string, Json> instead of a Map<string, Json>. The usage is simpler by not having to use Map.ofList:

[ "id", String "user/42"
  "firstName", String "John"
  "lastName", String "Doe" ]
|> Object

The biggest problem with a Map is, that the field names get sorted alphabetically. This shouldn't be a big problem for the deserialization of the generated Json object, but you can't enforce the order of your object fields. So in the above example, "firstName" becomes the first field.

Drawback of a list is, that you could provide by accident a second field name with the exact same name (in a map, the last one wins), but as the writer of the serializer you are responsible for a correct serialization of your object anyways.