Closed DunetsNM closed 4 months ago
This is by-design.
Actually the default codec for maps where the key is a string is Codecs.propMap
, change:
let! m = jreqWith (Codecs.map Codecs.string Codecs.int) "M" (fun x -> Some x.M)
for
let! m = jreqWith (Codecs.propMap Codecs.int) "M" (fun x -> Some x.M)
and you'll get all 3 representations identical.
In fact, in previous versions of Fleece there was no codec for "generic" maps, this was added in this latest version and the default was special-cased for the case where the keys are strings.
Maps codecs who's keys are string were always used as a key component of the library which is the map representation of a json object, where the strings are the names of the properties and the value is the IEncoding. As said above, this is called propMap
.
default codec for maps where the key is a string is Codecs.propMap
hmm, that's interesting. So for other types of keys it is just Codecs.map, right? Anyway, closing since it is by design.
Yes, the default for non string ones is the generic. It is by design, however if you feel like it's not a good design and have a better alternative we're all ears.
Consider following code, where I have three types with slightly different styles of Map field codec
I would expect it to print three identical json lines. However this is what it actually prints:
Apparently
Codecs.map Codecs.string Codecs.int
anddefaultCodec<_, Map<string, MyInterface>>
(or its implicit equivalentjreq
) produce different json output: in first case it's a list of key-value tuples, while in the second case it's a JSON object.Is it a bug or by design? If a bug then there's no simple fix given that changing behaviour for either of the codecs is a breaking change. Perhaps only obsoleting
Codecs.map
and introducing two different functions instead of it ?