You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Reading a record with a [<JsonIgnore>] field of a value type throws NullReferenceException #217
JsonSerializer.Deserialize throws NullReferenceException for a record that has a field of a value type marked with [<JsonIgnore>].
Repro
FSharp.SystemTextJson 1.4.36, .NET 10:
#r "nuget: FSharp.SystemTextJson, 1.4.36"openSystem.Text.JsonopenSystem.Text.Json.SerializationtypeWithIgnoredInt={ Id :string;[<JsonIgnore>] Count :int; Name :string }letoptions= JsonFSharpOptions.Default().ToJsonSerializerOptions ()
JsonSerializer.Deserialize<WithIgnoredInt>("""{"Id":"1","Name":"n"}""", options)// System.NullReferenceException: Object reference not set to an instance of an object.
The same record with [<JsonIgnore>] Note : string instead reads fine, and Note is null.
Cause
In Record.fs, defaultFields starts as Array.zeroCreate fieldCount, and only the fields whose DefaultValue is ValueSome get a value. An ignored field of a value type such as int has no default value, so its slot stays null, and the record constructor cannot unbox null to int.
Expected
The record gets the default value of the field type for an ignored field (0 for int), as it already gets null for an ignored field of a reference type.
Context
FSharp.Azure.Cosmos refuses such a field in its graph model until a release of this library reads the record (fsprojects/FSharp.Azure.Cosmos).
Activity