FSharp.SystemTextJson icon indicating copy to clipboard operation
FSharp.SystemTextJson copied to clipboard

Allow overriding generic types

Open Tarmil opened this issue 2 years ago • 0 comments

Allow using typedefof to override options for all instances of a generic type. Example:

let options =
    FSharpJsonOptions()
        .WithOverrides(fun o -> dict [ typedefof<Result<_, _>>, o.WithUnionTagName("Result") ])
        .ToJsonSerializerOptions()

JsonSerializer.Serialize((Ok "test": Result<string, string>), o)
// --> {"Result":"Ok","Fields":["test"]}
  • Q: What should happen if a type is matched by both a generic and a specific override? A: Only the specific one is taken into account.

    Example:

    let options =
        FSharpJsonOptions()
            .WithOverrides(fun o -> dict [
                typedefof<Result<_, _>>, o.WithUnionTagName("GenericCase")
                typedof<Result<string, string>>, o.WithUnionTagName("SpecificFields")
            ])
            .ToJsonSerializerOptions()
    
    JsonSerializer.Serialize((Ok "test": Result<string, string>), o)
    // --> {"Case":"Ok","SpecificFields":["test"]}
    

Tarmil avatar Feb 12 '24 08:02 Tarmil