Tarmil / FSharp.SystemTextJson

System.Text.Json extensions for F# types
MIT License
329 stars 43 forks source link

Deserialize null or missing fields as None? #176

Closed BrianVallelunga closed 8 months ago

BrianVallelunga commented 1 year ago

I'm having a hard time figuring out exactly how to handle null or missing JSON fields when deserializing a record. My serializer settings are currently:

let serializerOptions =
    JsonFSharpOptions
        .FSharpLuLike()
        .WithAllowNullFields(true)
        .WithSkippableOptionFields(true)
        .ToJsonSerializerOptions()

I don't care much about how an Option type gets serialized. Either null or as a missing field is fine by me, but when deserializing I want both null and missing fields to deserialize as None. Right now I'm getting errors if null is present. Is there a magic set of options that gets me the behavior I want? Thanks for the help.

drhumlen commented 11 months ago

We're having the exact same problem. It would be nice if it treated null and absence the same way when deserializing (with the given options).

pkese commented 10 months ago

Same here.

martin-bmc commented 9 months ago

I have the same issue. In an API I want to allow a user to either not send an optional field or send it as null, However using the default options makes optional fields required and using WithSkippableOptionFieldswill set a null value to Some null. I would a configuration where they both would be None.

Also it seems like issue is similar to #151

Tarmil commented 8 months ago

This is released in v1.3.13:

JsonFSharpOptions.Default()
    .WithSkippableOptionFields(SkippableOptionFields.Always, deserializeNullAsNone = true)
BrianVallelunga commented 8 months ago

Thanks! I'm going to test this today.

BrianVallelunga commented 6 months ago

Works great, thanks