-
Notifications
You must be signed in to change notification settings - Fork 5k
Migrating from Newtonsoft.Json to System.Text.Json reduces quality of error messages #38269
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
For this particular case, it is because there is no case-sensitive matching property within the You could annotate your model with the expected name, or add a case-insensitive option to the serializer, and/or use a built-in/custom naming policy. public class Model
{
public Guid Id { get; set; }
public int Integer { get; set; }
[JsonPropertyName("string")]
public string String { get; set; }
} OR var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
Model model = System.Text.Json.JsonSerializer.Deserialize<Model>(jsonString, options); OR var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
Model model = System.Text.Json.JsonSerializer.Deserialize<Model>(jsonString, options); And then you get the exception:
|
Good point. But I don't think machines count line numbers from zero either:-D Looks like a bug to me. |
Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process. This process is part of the experimental issue cleanup initiative we are currently trialing in a limited number of areas. Please share any feedback you might have in the linked issue. |
This is just great. Even today, STJ lacks many critical features from Newtonsoft and is still full of bugs and design flaws. Lots of issues were opened by the community, which often got a response along the lines of "we'll wait and see if more people need this." But you forgot that those issues get auto-locked or auto-closed, so no one can upvote them anymore today. We're all very disappointed with .NET 6, which should have addressed the big STJ issues above all else. Traditionally, it always takes Microsoft a few versions before getting it right, so the masses wait for that. You should have gotten it right by now. If we could upvote, you'd be shocked by the number of developers struggling with adopting STJ in real projects. But now all innovation has stopped in Newtonsoft and we are stuck with an inferior replacement. Thanks a lot! |
From the OP I can deduce the following actionable suggestions:
|
Another actionable thing would be to have 'public' messages on JsonException (probably as a new property). Today it mentions 'change the reader options' (trailing comma error), full type names and other such internal concerns. It's hardly feasible to have to manually match every json exception message and build a nice one from constituent parts again. Asp.net core mvc model binding is also notorious for spewing internal details like that. Having a nullable property for a public message would help a lot, we'll defer to a generic message if it's null. |
@blowdart the issue of policy around sensitive info in exception messages again. |
How about adding a boolean property Then anyone can decide when its appropriate to turn it on, for example:
|
We are in the process of migrating from
Newtonsoft.Json
toSystem.Text.Json
following similar steps as described here.Unfortunately
System.Text.Json
exception messages are of lower quality than the corresponding exception messages produced byNewtonsoft.Json
. In our ASP.NET Core application this gets exacerbated for the validation errors and the end result is an API which is less user friendly. Please see this issue for more details.In the following examples we try to deserialize a JSON string to this class:
For the different
jsonString
values listed below, this code was executed:Example 1:
Example 2:
Example 3:
All of these error messages are now harder to understand. In addition, the paths are less helpful and humans do not start counting line numbers from zero. It would be great if
System.Text
could produce exception error messages that were of the same quality asNewtonsoft.Json
.Also, with this JSON string:
then there is no exception thrown for neither
Newtonsoft.Json
norSystem.Text.Json
, but the deserialized values differ:model.string
is"2"
forNewtonsoft.Json
(this kind of leniency and the lack of a strict mode inNewtonsoft.Json
is one the reasons we wanted to migrate in the first place). However,System.Text.Json
leavesmodel.string
asnull
, which is odd (I would have expected an exception here). In the ASP.NET Core application this case results in a validation error when usingSystem.Text.Json
.The text was updated successfully, but these errors were encountered: