Tarmil / FSharp.SystemTextJson

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

[#162] Allow choosing the map format as object or array-of-pairs #169

Closed Tarmil closed 10 months ago

Tarmil commented 11 months ago

This fixes #162 by introducing the following format option:

type MapFormat =
    /// Always serialize Maps as JSON objects.
    /// Only supports the same key types as the built-in serializer for Dictionary.
    /// Requires System.Text.Json 8.0 or newer.
    | Object = 0
    /// Always serialize Maps as JSON arrays whose items are [key, value] JSON arrays.
    /// Supports all key types.
    | ArrayOfPairs = 1
    /// Serialize Maps as JSON objects if the key type is string or a single-case union wrapping string;
    /// otherwise, serialize Maps as JSON arrays whose items are [key, value] JSON arrays.
    /// Supports all key types.
    /// This is the default format.
    | ObjectOrArrayOfPairs = 2

Note: Object requires System.Text.Json 8.0 due to https://github.com/dotnet/runtime/issues/77326.