Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding generic types #180

Open
Tarmil opened this issue Feb 12, 2024 · 0 comments
Open

Allow overriding generic types #180

Tarmil opened this issue Feb 12, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@Tarmil
Copy link
Owner

Tarmil commented Feb 12, 2024

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"]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant