Skip to content

Commit

Permalink
Port/workaround IgnoreNullValues changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bartelink committed Sep 9, 2021
1 parent 11894b1 commit cfe0b4f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/FsCodec.SystemTextJson/Options.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ open System.Runtime.InteropServices
open System.Text.Json
open System.Text.Json.Serialization

#nowarn "44" // see IgnoreNullValues below

type Options private () =

static let defaultConverters : JsonConverter[] = [| |]
Expand All @@ -29,7 +31,7 @@ type Options private () =
if converters <> null then converters |> Array.iter options.Converters.Add
if indent then options.WriteIndented <- true
if camelCase then options.PropertyNamingPolicy <- JsonNamingPolicy.CamelCase; options.DictionaryKeyPolicy <- JsonNamingPolicy.CamelCase
if ignoreNulls then options.IgnoreNullValues <- true
if ignoreNulls then options.IgnoreNullValues <- true // options.DefaultIgnoreCondition <- JsonIgnoreCondition.Always is outlawed so nowarn required
if unsafeRelaxedJsonEscaping then options.Encoder <- System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
options

Expand Down
4 changes: 2 additions & 2 deletions src/FsCodec.SystemTextJson/UnionConverter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ type UnionConverter<'T>() =
match fieldInfos with
| [| fi |] when not (Union.typeIsUnionWithConverterAttribute fi.PropertyType) ->
match fieldValues.[0] with
| null when options.IgnoreNullValues -> ()
| null when options.DefaultIgnoreCondition = Serialization.JsonIgnoreCondition.Always -> ()
| fv ->
let element = JsonSerializer.SerializeToElement(fv, options)
match element.ValueKind with
Expand All @@ -129,7 +129,7 @@ type UnionConverter<'T>() =
element.WriteTo writer
| _ ->
for fieldInfo, fieldValue in Seq.zip fieldInfos fieldValues do
if fieldValue <> null || not options.IgnoreNullValues then
if fieldValue <> null || options.DefaultIgnoreCondition <> Serialization.JsonIgnoreCondition.Always then
writer.WritePropertyName(fieldInfo.Name)
JsonSerializer.Serialize(writer, fieldValue, options)

Expand Down
2 changes: 1 addition & 1 deletion tests/FsCodec.NewtonsoftJson.Tests/UnionConverterTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ let includeNullsProfile = Options.Create(ignoreNulls = false)
let includeNullsProfile = Settings.CreateDefault(OptionConverter() (*, ignoreNulls=false*))
#endif
[<DomainProperty(MaxTest=1000)>]
let ``UnionConverter ignoreNulls Profile roundtrip property test`` (x: TestDU) =
let ``UnionConverter includeNulls Profile roundtrip property test`` (x: TestDU) =
let ignoreNulls, profile = false, includeNullsProfile
assertIgnoreNullsIs false profile
roundtripProperty ignoreNulls profile x
Expand Down

0 comments on commit cfe0b4f

Please sign in to comment.