Tarmil / FSharp.SystemTextJson

System.Text.Json extensions for F# types
MIT License
329 stars 43 forks source link

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

Open vitash opened 8 months ago

vitash commented 8 months ago

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 commented 8 months ago

.WithAllowOverride() should do what you need here.

vitash commented 7 months ago

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!