-
Notifications
You must be signed in to change notification settings - Fork 5k
[System.Text.Json] Empty string is not deserializing to null #34310
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
Comments
An empty string is not valid JSON according to the spec so the error message is technically correct or am I missing something here? |
@AronParker Then why |
As @AronParker just pointed out, the reason why
I am not very familiar with ASP.NET; could you please provide a repro to easily understand your scenario? Maybe there is something interfering with serialization since that is not the default behavior of |
I am unable to reproduce this behavior of serializing null returning an empty string. var s = JsonSerializer.Serialize<string>(null);
Debug.Assert(s == "null" && s != ""); Would you mind providing a minimal working example to reproduce that behavior? |
Okay! Conside the following ASP.NET Core Web API controller action method:
Now call the above API method as follows:
|
I am unable to test ASP.NET Core Web related code right now but if |
Thanks for the code-snippet. If you check your response status; it contains I don't think that ASP.NET might have to change null serialization in order to return the proper bytes to represent a |
Now this makes me wonder, how should the new JSON extension for |
This is actually a bug in ASP.NET Core that we plan on addressing. dotnet/aspnetcore#8847 @pranavkm - do we have a good solution/workaround for this until we fix the issue? |
I think we should also think about what the behavior was in WebApiClient. If we converted 204 to |
@rynowak thanks. |
@rynowak It wold be great if this is fixed in |
The workaround here - dotnet/aspnetcore#8847 (comment) works reasonably well, although it has the side-effect of affecting the behavior when a value (not an IActionResult) is returned from the action (eg. |
Empty string is not deserializing to null in
System.Text.Json
. Look at the following code:In the above code
responseString
is getting an empty string because API Controller method returning null and hence so after deserializing theresponseString
employeeDetails
in the above code should be null but instead it's throwing the following exception:The text was updated successfully, but these errors were encountered: