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
- Adding unit test to cover NullReferenceException when passing null to a collaction (issue #37078) — committed to Jozkee/corefx by Jozkee 5 years ago
- Adding unit test to cover NullReferenceException when passing null to a collection (#38566) * Adding unit test to cover NullReferenceException when passing null to a collaction (issue #37078) * Ad... — committed to dotnet/corefx by Jozkee 5 years ago
- Adding unit test to cover NullReferenceException when passing null to a collection (#38566) * Adding unit test to cover NullReferenceException when passing null to a collaction (issue #37078) * Ad... — committed to steveharter/dotnet_corefx by Jozkee 5 years ago
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
NB
JsonSerializer
now is underusing 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.