FSharp.SystemTextJson icon indicating copy to clipboard operation
FSharp.SystemTextJson copied to clipboard

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

Open vitash opened this issue 2 years ago • 2 comments

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?

vitash avatar Mar 25 '24 03:03 vitash

.WithAllowOverride() should do what you need here.

Tarmil avatar Mar 28 '24 08:03 Tarmil

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!

vitash avatar May 03 '24 07:05 vitash