Skip to content

NullReferenceException when Deserializing with null PropertyNamingPolicy option #31105

@cedemax

Description

@cedemax

Deserializing a complex object (I could not replicate the error with a dummy object) with a null PropertyNamingPolicy throws NullReferenceException.

My code :

var a = JsonSerializer.Deserialize<T>(jsonString,options);  // options.PropertyNamingPolicy = null

Stack trace:

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=System.Text.Json
  StackTrace:
   at System.Text.Json.JsonPropertyInfo.Read(JsonTokenType tokenType, ReadStack& state, Utf8JsonReader& reader) in /_/src/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfo.cs:line 345
   at System.Text.Json.JsonSerializer.HandleValue(JsonTokenType tokenType, JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& state) in /_/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleValue.cs:line 28
   at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack) in /_/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.cs:line 50
   at System.Text.Json.JsonSerializer.ReadCore(Type returnType, JsonSerializerOptions options, Utf8JsonReader& reader) in /_/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Helpers.cs:line 22
   at System.Text.Json.JsonSerializer.ParseCore(String json, Type returnType, JsonSerializerOptions options) in /_/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.String.cs:line 73
   at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options) in /_/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.String.cs:line 28

The jsonString

{"General":{"InnerRegionMode":"Hole"},"File":{"Format":"svg","PreferZip":false,"PreferOneFile":false,"SupportedFormats":[["svg"]]},"Coordinates":{"System":{"Projection":{"Name":"ETRS-TM35FIN"},"Scale":1000.0,"Origin":{"X":0.0,"Y":0.0,"Z":0.0},"Unit":{"UnitOf":2,"Name":"millimetre","Scale":0.001,"Symbol":"mm"}},"Flatten":false,"Centering":false,"Grids":null,"OneMetre":1.0,"OneMillimetre":0.001,"OneCentimetre":0.01},"Terrain":{"Exaggerate":2.5,"Contours":{"Closed":true,"Delta":0.5,"Reduce":0.1,"Smooth":0.0,"MinArea":9.0,"Highlights":{"N":10,"OutlineColor":"rgba(0,0,0,0.54)","FillColor":null,"StrokeWidth":0.9,"DataStyle":"Outline2D","Enabled":true,"Layer":null,"Flatten":false,"ClipMode":0},"Labels":1,"LabelSpacing":50.0,"IndividualLayers":false,"OutlineColor":"rgba(0,0,0,0.29)","FillColor":null,"StrokeWidth":0.5,"DataStyle":"Outline2D","Enabled":true,"Layer":null,"Flatten":false,"ClipMode":0},"Mesh":null,"Compression":{"InnerDensity":1.0,"OuterDensity":1.0,"DensityProfile":"Flat","InnerRegionTemplateId":null,"Enabled":false,"Layer":null,"Flatten":false,"ClipMode":0},"Texture":{"Colors":["rgba(69,114,45,1)","rgba(144,160,110,1)","rgba(195,160,105,1)","rgba(243,243,243,1)"],"ColorMap":{"Colors":["69, 114, 45","144, 160, 110","195, 160, 105","243, 243, 243"],"WaterColor":"","Contrast":0.0,"Min":0.0,"Max":1.0,"IsClear":false},"FlatColor":"DarkGray","Style":"ColorMap","SupportedStyles":null,"Enabled":true,"Layer":null,"Flatten":false,"ClipMode":0},"CellLimit":0.0,"Enabled":true,"Layer":null,"Flatten":false,"ClipMode":0},"Shores":{"GuessDepth":false,"OutlineColor":"rgba(0,100,200,0.59)","FillColor":"rgba(66,164,194,0.48)","StrokeWidth":1.0,"DataStyle":"Outline2D","Enabled":true,"Layer":null,"Flatten":false,"ClipMode":0},"Roads":{"DefaultWidth":3.0,"ExtraWidth":0.0,"OutlineColor":"rgba(53,51,51,0.92)","FillColor":"rgba(184,167,167,1)","StrokeWidth":1.0,"DataStyle":"Outline2D","Enabled":true,"Layer":null,"Flatten":false,"ClipMode":0},"Buildings":{"GuessHeight":false,"ManualHeight":0.0,"ShowID":false,"OutlineColor":"rgba(0,0,0,1)","FillColor":"rgba(138,138,138,1)","StrokeWidth":1.0,"DataStyle":"Outline2D","Enabled":true,"Layer":null,"Flatten":false,"ClipMode":0},"Properties":{"Text":{"ProportionalSize":true,"MinSize":1.0,"MaxSize":50.0,"OutlineColor":"rgba(0,0,0,1)","FillColor":"rgba(255,255,255,1)","StrokeWidth":4.0,"DataStyle":"Outline2D","Enabled":true,"Layer":null,"Flatten":false,"ClipMode":0},"Markers":{"ShowAccuracyBounds":true,"ShowID":false,"OutlineColor":"rgba(255,0,255,1)","FillColor":null,"StrokeWidth":0.3,"DataStyle":"Outline2D","Enabled":false,"Layer":null,"Flatten":false,"ClipMode":0},"OutlineColor":"rgba(132,35,221,0.79)","FillColor":null,"StrokeWidth":1.0,"DataStyle":"Outline2D","Enabled":true,"Layer":null,"Flatten":false,"ClipMode":0}}

The type it maps to has a lot of code so I won't be posting it here. The deserialization 'works', but everything is null after deserialization when using JsonNamingPolicy.CamelCase.
If using PropertyNameCaseInsensitive = true and JsonNamingPolicy.CamelCase then it will again throw NullReferenceException on the same line as before...

EDIT:

This seemed to be caused by having a custom JsonConverter<System.Drawing.Color?>, but no JsonConverter<System.Drawing.Color> defined... The complex class contained both Color and Color? objects. Adding the non-nullable version fixed the issue. The error was quite unhelpful however.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions