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

Untagged Attribute and options configurations don't seem to be working #183

Open
vitash opened this issue Mar 25, 2024 · 2 comments
Open

Comments

@vitash
Copy link

vitash commented Mar 25, 2024

I am writing an asp.net webapi program and in the setup section I added Services.AddJsonOptions( ... JsonFSharpOptions). The options is WithUnionInternalTag, which is used for default serialization. But for individual types, you need to use Untagged serialization.
I found that [<JsonFSharpConverter(UnionEncoding=(JsonUnionEncoding.Untagged))>] Attribute doesn't seem to work.

Code version information:

    <TargetFramework>net8.0</TargetFramework>

    <PackageReference Include="FSharp.SystemTextJson" Version="1.2.42" />

Test Code:

[<JsonFSharpConverter(UnionEncoding=(JsonUnionEncoding.Untagged))>]
type Untagged1 = A of int | B of  string

[<Fact>]
let ``test tag`` () =
    let optInternalTag =
        JsonFSharpOptions()
                    .WithUnionInternalTag() // <-
                    .WithUnionAllowUnorderedTag()
                    .WithUnionUnwrapFieldlessTags()
                    .WithUnwrapOption()
                    .ToJsonSerializerOptions()

    let b = B "bbb"
    let jsonB = JsonSerializer.Serialize(b, optInternalTag)
    let valueB = JsonSerializer.Deserialize<Untagged1>("bbb", optInternalTag)

image


How should I configure it so that I can use options as the default serialization and Attribute as the individually specified serialization in an asp.net application?

@Tarmil
Copy link
Owner

Tarmil commented Mar 28, 2024

.WithAllowOverride() should do what you need here.

@vitash
Copy link
Author

vitash commented May 3, 2024

Hi there, I've used other solutions before, and now that I've had time to continue testing the solution you mentioned WithAllowOverride, I've found that it doesn't work as expected either!

#r "nuget: FSharp.SystemTextJson, 1.3.13"

open System.Text.Json.Serialization
open System.Text.Json

[<JsonFSharpConverter(UnionEncoding=(JsonUnionEncoding.Untagged))>]
type Untagged1 = A of int | B of  string

let optInternalTag =
    JsonFSharpOptions()
        .WithUnionInternalTag() // <-
        .WithUnionAllowUnorderedTag()
        .WithUnionUnwrapFieldlessTags()
        .WithUnwrapOption()
        .WithAllowOverride() // add
        .ToJsonSerializerOptions()

let valB = B "bbb"
let jsonB = JsonSerializer.Serialize(valB, optInternalTag)
jsonB |> printfn "%A" // expect: jsonB = "bbb"; actual: jsonB = "{"Item":"bbb"}"

let valueB = JsonSerializer.Deserialize<Untagged1>("bbb", optInternalTag)
//  ^ System.Text.Json.JsonException: 'b' is an invalid start of a value. 
//           Path: $ | LineNumber: 0 | BytePositionInLine: 0.

This is a very nice library, thanks for the code!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants