Skip to content

Allow overriding generic types #180

Closed

Description

@Tarmil

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"]}

Activity

  1. added a commit that references this issue on Mar 30, 2024
  2. added 3 commits that reference this issue on Dec 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions