-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[System.Text.Json] System.InvalidProgramException when deserializing json. #36621
Comments
cc: @jozkee @steveharter |
I think I figured it out, It seems to be the readers in my converters for some reason. The switch expressions and even doing manual No clue why as it expects the type returned by the Read method. Odd thing about this is that the code seems valid to me. Edit: It seems the issue was that I defined 1 enum value in a class that gets serialized to as nullable. |
Reopening this issue. System.Text.Json team, can you take a look at this? The The failure is happening on line 174 in the snippet below. The first time this particular Set delegate is invoked, the JIT tries to compile the IL, and the JIT itself is throwing the exception. runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfoOfT.cs Lines 167 to 177 in 69f152a
|
I think another issue is there also is no form to override on Read where the return value can be nullable if CanConvert does: public override bool CanConvert(Type t)
=> t == typeof(T) || t == typeof(T?); Like there is no public T? Read(ref Utf8JsonReader reader, Type typeToConvert,
JsonSerializerOptions options) variant of Read. |
I've additionally encountered this with deserialization too for Schema.NET (RehanSaeed/Schema.NET#100) with Preview 4 of System.Text.Json. It seems that all of the errors generated in our tests are for the same bit of invalid IL code generated by System.Text.Json.
|
One thing that might be of note, in my case it doesn't seem to be limited to value types being nullable, we have the error also happening on type CI Test Result: https://ci.appveyor.com/project/RehanSaeed/schema-net/builds/33064765/job/62lxs509wsfre175#L2698 |
FYI, just tested Preview 6 out of a hope that this might have been co-incidentally fixed. Unfortunately this issue still occurs in the same way as outlined before. eg. |
Just tested out Preview 7, now with a related (but not the same) error.
|
@Turnerj - do you have project/code sample that you can share that can repro this error? |
@layomia - that latest error was generated by the latest commit in this PR: RehanSaeed/Schema.NET#100 The test that failed in the latest error was this one: https://github.com/Turnerj/Schema.NET/blob/5df8731a317c5173a1ace4870188ef82b270c95a/Tests/Schema.NET.Test/core/ItemListTest.cs#L59-L63 My various other comments earlier about IL errors are also from the same PR on earlier preview versions. |
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 ( If you want to override null, override |
👍 If the nullable version is included in the |
Hmmmmm - in my case, my public override bool CanConvert(Type typeToConvert) => typeof(IValues).IsAssignableFrom(typeToConvert); The types that implement I'll note that this converter is specifically added to every property where it is applicable so I don't know if |
Now that this is closed, should I be creating a new issue for the |
@Turnerj, yes pease open a new issue and kindly provide a repro that shows the converter, all types being (de)serialized, what the JSON payload looks like, and what the call to the serializer looks like. |
Description
Whenever I try to deserialize this json with System.Text.Json it throws an exception in
System.Text.Json.dll
asSystem.InvalidProgramException
, then it duplicates the throw inSystem.Private.CoreLib.dll
, thenSystem.Private.CoreLib.dll
throws anSystem.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:
It should throw that exception.
Note:
json
is a string returned fromReadAsStringAsync()
from anHttpResponseMessage
from my code, however for this test have it be the string fromFile.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.
The text was updated successfully, but these errors were encountered: