Open vitash opened 8 months ago
.WithAllowOverride()
should do what you need here.
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!
I am writing an asp.net webapi program and in the setup section I added
Services.AddJsonOptions( ... JsonFSharpOptions)
. The options isWithUnionInternalTag
, which is used for default serialization. But for individual types, you need to useUntagged
serialization. I found that[<JsonFSharpConverter(UnionEncoding=(JsonUnionEncoding.Untagged))>]
Attribute doesn't seem to work.Code version information:
Test Code:
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?