Skip to content

Allow disabling the handling of types already handled by System.Text.Json #126

@Tarmil

Description

@Tarmil

As of .NET 6, a number of F# types are now handled as built-in by System.Text.Json:

  • Records: serialization has always worked, and deserialization now works as one would expect, as a side-effect of the added support for non-default constructors for C# records.
  • list, Set: as a JSON array, like FSharp.SystemTextJson.
  • Map: always as an object, even when the key is not string, and throws an exception if the key type is complex (eg it works with int, but throws with a record). Unlike FSharp.SystemTextJson which uses an object for string keys and an array of arrays otherwise.
  • option, voption: as null for None/ValueNone and the plain value for Some/ValueSome, like FSharp.SystemTextJson by default. However, there is no customizability to wrap the Some value instead.

I think that the only F# types that System.Text.Json still can't handle by default are unions and complex-keyed maps.

We should add an option to not handle these and let System.Text.Json take care of them instead. Maybe even an enum that lets the user choose which types they want handled by FSharp.SystemTextJson and which ones they want to leave up to System.Text.Json:

type JsonFSharpTypes =
    | Records = 0x1
    | Unions = 0x2
    | Lists = 0x4
    | Sets = 0x8
    | Maps = 0x10
    | Options = 0x20
    | ValueOptions = 0x40
    // Combinations:
    | OptionalTypes = 0x60 // option and voption
    | Collections = 0x1C // list, set and map
    | Minimal = 0x12 // all types not already fully supported by System.Text.Json
    | All = 0xffffffff

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions