runtime: [System.Text.Json] System.InvalidProgramException when deserializing json.

Description

Whenever I try to deserialize this json with System.Text.Json it throws an exception in System.Text.Json.dll as System.InvalidProgramException, then it duplicates the throw in System.Private.CoreLib.dll, then System.Private.CoreLib.dll throws an System.AggregateException The Json data below has been changed from what was requested from an online API. So expect values not match the real user ids on the api I randomized it all so you guys get an idea what the api can and will give out.

the json: https://paste.mod.gg/ipiwosexuv.json And the code, I am posting it as a url to avoid making this message too long as well. The code is: https://paste.mod.gg/fokaxaxele.cpp (the thing placed it as .cpp when it is c#)

Now when you call it like so:

var result = JsonSerializer.Deserialize<MembersRoot>(json, Converter.Settings);

It should throw that exception. Note: json is a string returned from ReadAsStringAsync() from an HttpResponseMessage from my code, however for this test have it be the string from File.ReadAllText() and that json data as the file.

Configuration

.net core 3.1, It uses the latest pre-release System.Text.Json as I was told somewhere that the 3.1 version was broken so I tried to 5.0 pre-release one to see if it works. I am using the latest insider preview of Windows 10 and it is x64, the program targets Any CPU with no preference at all. I tried this in debug mode in VS2019 that is when I noticed the exception and the program not doing what it should do when it did not have this exception (that was when it used Newtonsoft.Json’s DeserializeObject which worked fine for this).

I do not think it is specific to the configuration but something wrong on either System.Text.Json side, or something wrong with my converters or something.

Regression?

I do not know for sure.

Other information

I do not know yet of any workarrounds other than using Newtonsoft.Json which I would like to squash out for System.Text.Json. 1 less package means for me faster restore which means maybe slightly faster build.

Sadly I only got what VS2019 printed, it never gave me the stack traces to the exceptions.

Exception thrown: 'System.InvalidProgramException' in System.Text.Json.dll
Exception thrown: 'System.InvalidProgramException' in System.Private.CoreLib.dll
Exception thrown: 'System.AggregateException' in System.Private.CoreLib.dll

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 16 (8 by maintainers)

Most upvoted comments

For the original case there is a custom converter:

    internal class PatronStatusConverter : JsonConverter<PatronStatus>
    {
        /// <inheritdoc />
        public override bool CanConvert(Type t)
            => t == typeof(PatronStatus) || t == typeof(PatronStatus?);

A custom converter should not include the nullable version (PatronStatus?) in CanConvert as that is taken care of automatically by the serializer, at least if the standard “null” semantics are desired.

If you want to override null, override HandleNull and return true from the converter. This feature was added in 5.0.