Skip to content

Can't parse Option<'T> from a json field that's either missing or null #182

@pkese

Description

@pkese

What's the required set of options that gets the following code to work?

Either I'm getting: Missing field for record type
or The JSON value could not be converted to System.Double. Cannot get the value of a token type 'Null' as a number.

#r "nuget: FSharp.SystemTextJson, 1.2.42"
open System.Text.Json
open System.Text.Json.Serialization

let options =
    JsonFSharpOptions.Default()
        // Add any .WithXXX() calls here to customize the format
        .WithSkippableOptionFields()
        .WithAllowNullFields()
        //.WithUnwrapOption()
        .ToJsonSerializerOptions()

type Doc = { x: float option }

let json = """
    [
        {"x": 1.0},
        {"x": 2},
        {"x": null},
        {}
    ]
"""

JsonSerializer.Deserialize<Doc[]>(json, options)
|> printfn "%A"

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