-
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
ArgumentNullException in Json serialization #35422
Comments
Tagging subscribers to this area: @jozkee |
@iSazonov it is indeed odd that you are seeing |
Today we moved to 5.0 Preview4 in PowerShell repo and I will re-check.
Yes, we are trying to serialize null. It worked before but not in 5.0. |
@layomia After we update to latest .Net 5.0 preview I don't see Exception :
Type : System.ArgumentNullException
Message : Value cannot be null. (Parameter 'inputType')
ParamName : inputType
TargetSite :
Name : Serialize
DeclaringType : System.Text.Json.JsonSerializer
MemberType : Method
Module : System.Text.Json.dll
StackTrace :
at System.Text.Json.JsonSerializer.Serialize(Object value, Type inputType, JsonSerializerOptions options) PowerShell 6.0 and 7.0 use NewtonSoft Json.Net and follow works: > $null | ConvertTo-Json -Compress
null But after we move to .Net 5.0 Json API we get the exception.
Expectation is that follow works: JsonSerializer.Serialize(null, null, null) // returns "null"
JsonSerializer.Serialize(null, typeof(string), null) // returns "null" |
Glad to hear! Thanks.
So the expectation is that when the instance to serialize is Why pass a |
Yes.
PowerShell scenario JsonSerializer.Serialize(objectToProcess, objectToProcess?.GetType(), options); Again, my request comes from only the scenario and I could create a workaround (use |
Yes, we consider passing a
Doing a null check here is probably the best option, as the serializer will simply write I'll go ahead and close this issue, since it is not actionable as is. Please feel free to re-open if you still face issues. |
@layomia Thanks! I will implement the way. I hope it will be in next PowerShell Preview and we will get more feedback. |
@layomia since ASP.NET had to react to that, do you think it is worth a breaking change note? so it appears in https://docs.microsoft.com/en-us/dotnet/core/compatibility/3.1-5.0#core-net-libraries |
@danmosemsft - yes, it is a breaking change and has been shown to be significant. The change was made in #528. Will file a breaking change note. |
thanks! |
Update: a breaking change doc was already filed for this: dotnet/docs#20823, https://docs.microsoft.com/en-us/dotnet/core/compatibility/3.1-5.0#jsonserializerserialize-throws-argumentnullexception-when-type-parameter-is-null. |
In PowerShell repo we get an exception for null parameter:
I tried to directly reference System.Text.Json 5.0.0-preview.2.20160.6 but get the same exception.
I wonder to see JsonSerializer.VerifyValueAndType() because it was removed it in #2259
Now we moved to 5.0 Preview3 with the same error.
The text was updated successfully, but these errors were encountered: