Skip to content

Inconsistent behavior of nulls? #151

@GBirkel

Description

@GBirkel

Now that WithSkippableOptionFields is implemented (thank you!) I can generate what looks like confusing results:

The structure I'm deserializing:
      { "stringOption": null,
        "seqOfStringOption1": [null],
        "seqOfStringOption2": []
       }

Deserializing with WithSkippableOptionFields:

{ stringOption = Some null
  seqOfStringOption1 = seq [None]
  seqOfStringOption2 = seq [] }

Deserializing without WithSkippableOptionFields:

{ stringOption = None
  seqOfStringOption1 = seq [None]
  seqOfStringOption2 = seq [] }

What looks strange to me is the first stringOption. Passing null renders "Some null". How do I get "None" instead? Is my only choice to omit the field from the input? If so, why the inconsistency between that, and a null that appears inside a sequence?

The code I used:

#r "nuget: FSharp.SystemTextJson"

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

type TestInput = {
    stringOption: string option
    seqOfStringOption1: string option seq
    seqOfStringOption2: string option seq
}

let skipYes = JsonFSharpOptions.Default()
                    .WithSkippableOptionFields()
                    .ToJsonSerializerOptions()

let skipNo = JsonFSharpOptions.Default()
                    .ToJsonSerializerOptions()

let data = """
                { "stringOption": null,
                  "seqOfStringOption1": [null],
                  "seqOfStringOption2": []
                 }"""
printfn "\n%s\n" data

printfn "\nDeserializing with WithSkippableOptionFields:\n"
let deserializedA = JsonSerializer.Deserialize<TestInput>(data, skipYes)
printfn "%A" deserializedA

printfn "\nDeserializing without WithSkippableOptionFields:\n"
let deserializedB = JsonSerializer.Deserialize<TestInput>(data, skipNo)
printfn "%A" deserializedB

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions