Skip to content

Exception when attempting to deserialize POCO using Json.NET  #16346

Closed
@rdavisau

Description

@rdavisau

Great work on this project! I've been having a play with Blazor recently and run into a hitch when trying to use it in conjunction with Json.NET. I know that the cause behind this could manifest anywhere between Blazor, Json.NET and the WASM bits so I apologise in advance if this isn't ultimately the right place to raise it. I won't be offended if you tell me to take this to another repo :)

Title

Exception when attempting to deserialize POCO using Json.NET

Functional impact

Per title

Minimal repro steps

  1. Create a new Blazor project using the ASP.NET Core Project wizard in Visual Studio 2017 Preview
  2. Install-Package Newtonsoft.Json
  3. Replace OnInitAsync in the FetchData component with below:
protected override async Task OnInitAsync()
{
    var emptyJson = "{ }";
    var json = "{ \"Summary\" : \"Value\" }";

    // all working
    Console.WriteLine($"JObject - {JsonConvert.DeserializeObject<JObject>(json)}");
    Console.WriteLine($"ExpandoObject - {JsonConvert.DeserializeObject<ExpandoObject>(json)}");
    Console.WriteLine($"Dictionary<string, object> - {JsonConvert.DeserializeObject<Dictionary<string, object>>(json)}");

    // no setter called, no problem
    Console.WriteLine($"POCO, no setters called - {JsonConvert.DeserializeObject<WeatherForecast>(emptyJson)}");

    // boom
    Console.WriteLine($"POCO, setter called - {JsonConvert.DeserializeObject<WeatherForecast>(json)}");
}
  1. Launch the project and navigate to Fetch data page
  2. Open console to see output

Expected result

All five calls succeed and display the .ToString() representation of the deserialized objects.

Actual result

4/5 calls succeed and exception is raised for the last.

Further technical details

It seems that POCOs fail to deserialize, specifically when a set operation is called, with the below output:

WASM: [Newtonsoft.Json.JsonSerializationException] Error setting value to 'Summary' on 'WebApplication5.Pages.FetchData+WeatherForecast'. ---> System.NullReferenceException: Object reference not set to an instance of an object. 
WASM: at Newtonsoft.Json.Serialization.ExpressionValueProvider.SetValue (System.Object target, System.Object value) <0x1cf1220 + 0x00086> in <d32db49e5e3440729da31845c03ddc3a>:0 
WASM: at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue (Newtonsoft.Json.Serialization.JsonProperty property, Newtonsoft.Json.JsonConverter propertyConverter, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerProperty, Newtonsoft.Json.JsonReader reader, System.Object target) <0x1cf0508 + 0x00158> in <d32db49e5e3440729da31845c03ddc3a>:0 
WASM: at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject (System.Object newObject, Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty member, System.String id) <0x1ce7cb8 + 0x003e8> in <d32db49e5e3440729da31845c03ddc3a>:0 
WASM: at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) <0x1c3d5b8 + 0x0029c> in <d32db49e5e3440729da31845c03ddc3a>:0 
WASM: at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) <0x1b9c508 + 0x000ca> in <d32db49e5e3440729da31845c03ddc3a>:0 
WASM: at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) <0x19646a0 + 0x001a6> in <d32db49e5e3440729da31845c03ddc3a>:0 
WASM: at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) <0x195ca10 + 0x000ac> in <d32db49e5e3440729da31845c03ddc3a>:0 
WASM: at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) <0x195c330 + 0x0001c> in <d32db49e5e3440729da31845c03ddc3a>:0 
WASM: at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) <0x19269d0 + 0x00060> in <d32db49e5e3440729da31845c03ddc3a>:0 
WASM: at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) <0x1cb3b20 + 0x00020> in <d32db49e5e3440729da31845c03ddc3a>:0 
WASM: at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value) <0x1cb3ae8 + 0x0000a> in <d32db49e5e3440729da31845c03ddc3a>:0 
WASM: at WebApplication5.Pages.FetchData+<OnInitAsync>d__2.MoveNext () <0x1925bd8 + 0x00096> in <2cb2c533ff6b4424901e788f72648786>:0 

I have also seen a more detailed stacktrace which I happen to have a screenshot of:
image

Versions

Microsoft.AspNetCore.Razor.Design 2.1.0-preview2-30230
Microsoft.AspNetCore.Blazor.Browser 0.1.0
Microsoft.AspNetCore.Blazor.Build 0.1.0
Newtonsoft.Json 11.0.2

netcore 2.1.300-preview1-008174

Visual Studio Enterprise 2017 Preview (15.7.0 Preview 2.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-blazorIncludes: Blazor, Razor Components

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions