Tarmil / FSharp.SystemTextJson

System.Text.Json extensions for F# types
MIT License
329 stars 45 forks source link

Allow handling arbitrary Map keys as JSON object properties #162

Closed Tarmil closed 1 year ago

Tarmil commented 1 year ago

Since .NET 6, the handling of dictionary keys can be customized using JsonConverter.Write/ReadAsPropertyName. Our Map converter should try use these methods in the same way, and only fall back to array-of-pairs in JSON if it can't.

Tarmil commented 1 year ago

It's actually not really possible to choose the format based on whether the key type's converter supports {Read|Write}AsPropertyName. There isn't a flag that tells whether they're supported; just these two methods, that will either succeed or fail when called.

So a better option would be to follow what @cmeeren suggests in #152 and allow choosing the format in JsonFSharpOptions.