Tarmil / FSharp.SystemTextJson

System.Text.Json extensions for F# types
MIT License
325 stars 44 forks source link

Treat single-case unions like the underlying type in Map keys #51

Closed Tarmil closed 4 years ago

Tarmil commented 4 years ago

Currently Map<string, V> is serialized as a plain JSON object. However, Map<K, V> where K is a single-case union containing a string is still treated like an arbitrary key type, and serialized as an array of [key, value] pairs. It should be serialized as an object instead.

type UserId = UserId of string

JsonSerializer.Serialize(Map [(UserId "Jane", 123); (UserId "John", 456)], options)
// Currently: [["Jane",123],["John",456]]
// Expected: {"Jane":123,"John":456}