runtime: NullReferenceException calling JsonSerializer.ReadAsync()

As per https://github.com/dotnet/corefx/issues/36611#issuecomment-485161258, I have an existing ASP.NET Core 2.2 application I’ve updated to use ASP.NET Core 3.0 preview 4 where I tried to plug in System.Text.Json into Refit’s IContentSerializer interface as a replacement for Newtonsoft.Json.

However, when trying to deserialize a JSON stream for various application models to their POCOs, a NullReferenceException is thrown.

System.NullReferenceException: Object reference not set to an instance of an object.
   at void System.Text.Json.Serialization.ReadStackFrame.SetReturnValue(object value, JsonSerializerOptions options, ref ReadStackFrame current, bool setPropertyDirectly)
   at bool System.Text.Json.Serialization.JsonSerializer.HandleNull(ref Utf8JsonReader reader, ref ReadStack state, JsonSerializerOptions options)
   at void System.Text.Json.Serialization.JsonSerializer.ReadCore(JsonSerializerOptions options, ref Utf8JsonReader reader, ref ReadStack state)
   at void System.Text.Json.Serialization.JsonSerializer.ReadCore(ref JsonReaderState readerState, bool isFinalBlock, Span<byte> buffer, JsonSerializerOptions options, ref ReadStack state)
   at async ValueTask<TValue> System.Text.Json.Serialization.JsonSerializer.ReadAsync<TValue>(Stream utf8Json, Type returnType, JsonSerializerOptions options, CancellationToken cancellationToken)
   at TResult System.Threading.Tasks.ValueTask<TResult>.get_Result()

A repro for the issue can be found here: https://github.com/martincostello/JsonSerializer-NullReferenceException-Repro

The object graph is probably more verbose that needed for a minimum LoC repro, but except for the models and properties being renamed and the JSON string simplified, this is the same as the application that presented the issue.

The essence of the code that throws is:

var json = "{ ... large JSON model with nested objects and null values ... }";
var buffer = Encoding.UTF8.GetBytes(json);
var utf8Json = new MemoryStream(buffer);
var options = new JsonSerializerOptions();

var model = await JsonSerializer.ReadAsync<RootObject>(utf8Json, options);

/cc @BrennanConroy

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 17 (17 by maintainers)

Commits related to this issue

Most upvoted comments

Just to confirm, I can’t replicate this with 3.0.100-preview6-012264.

no issue with nightly https://dotnetcli.blob.core.windows.net/dotnet/Sdk/master/dotnet-sdk-latest-win-x64.zip

D:\git\JsonSerializer-NullReferenceException-Repro (master -> origin)
λ dotnet-sdk-latest-win-x64\dotnet.exe --info
.NET Core SDK (reflecting any global.json):
 Version:   3.0.100-preview7-012284
 Commit:    16d9275044

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.17134
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   D:\git\JsonSerializer-NullReferenceException-Repro\dotnet-sdk-latest-win-x64\sdk\3.0.100-preview7-012284\

Host (useful for support):
  Version: 3.0.0-preview7-27806-02
  Commit:  90a101062a

.NET Core SDKs installed:
  3.0.100-preview7-012284 [D:\git\JsonSerializer-NullReferenceException-Repro\dotnet-sdk-latest-win-x64\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.App 3.0.0-preview7.19306.1 [D:\git\JsonSerializer-NullReferenceException-Repro\dotnet-sdk-latest-win-x64\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.0.0-preview7-27806-02 [D:\git\JsonSerializer-NullReferenceException-Repro\dotnet-sdk-latest-win-x64\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.0.0-preview7-27806-02 [D:\git\JsonSerializer-NullReferenceException-Repro\dotnet-sdk-latest-win-x64\shared\Microsoft.WindowsDesktop.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

D:\git\JsonSerializer-NullReferenceException-Repro (master -> origin)
λ dotnet-sdk-latest-win-x64\dotnet.exe test
Test run for D:\git\JsonSerializer-NullReferenceException-Repro\bin\Debug\netcoreapp3.0\JsonSerializer-NullReferenceException-Repro.dll(.NETCoreApp,Version=v3.0)
Microsoft (R) Test Execution Command Line Tool Version 16.0.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...

Total tests: 2. Passed: 2. Failed: 0. Skipped: 0.
Test Run Successful.
Test execution time: 1.3524 Seconds

D:\git\JsonSerializer-NullReferenceException-Repro (master -> origin)
λ

NB JsonSerializer now is under using System.Text.Json;

That’s probably https://github.com/dotnet/corefx/issues/36510

Let’s try to keep the issue on the null ref topic and open new issues if you find any.

I tried this out with newer nightly bits and can’t reproduce. I think this has been fixed since preview4.

You are using IList which I believe wasn’t supported until a few days ago, see https://github.com/dotnet/corefx/pull/36756 If you removed the list you might see the json being parsed without errors.