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

Add ctx.BindJson<'T>(jsonSerializerOptions) #34

Open
pkese opened this issue Dec 3, 2024 · 3 comments
Open

Add ctx.BindJson<'T>(jsonSerializerOptions) #34

pkese opened this issue Dec 3, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@pkese
Copy link

pkese commented Dec 3, 2024

It would be nice if context.BindJson would allow passing custom JsonSerializerOptions.

@Lanayx
Copy link
Owner

Lanayx commented Dec 3, 2024

@pkese have you seen the json custom configuration docs https://lanayx.github.io/Oxpecker/src/Oxpecker/#writing-json ?

services.AddSingleton<IJsonSerializer>(
        SystemTextJsonSerializer(specificOptions)) |> ignore

@pkese
Copy link
Author

pkese commented Dec 3, 2024

Yes, I have, but I have different parts of APIs that are expecting different types of data:
one part of my API is public, the other part is exposed to F# client sending Thoth.Json compliant data, so I need two sets of JsonSerializerOptions

@Lanayx
Copy link
Owner

Lanayx commented Dec 3, 2024

I see. I think that current helpers like BindJson are just for the most common scenarios, but it should be very easy to add customize, for example

module My =
    let anotherSerializer = SystemTextJsonSerializer(myOptions)

and then

[<Extension>]
static member WriteJson2<'T>(ctx: HttpContext, value: 'T) =
    My.anotherSerializer.Serialize(value, ctx, false)

or

[<Extension>]
static member BindJson2<'T>(ctx: HttpContext) =
    task {
        try
            return! My.anotherSerializer.Deserialize<'T>(ctx)
        with ex ->
            return raise <| ModelBindException("Unable to deserialize model from JSON", ex)
    }

so given this is easy achievable I'm reluctant to extend API for this case

@Lanayx Lanayx added the enhancement New feature or request label Dec 4, 2024
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

2 participants