Improving error message generation
See original GitHub issueHi!
I have a (hopefully simple) question about how to properly use this library. Apologies in advance if this is covered in the docs somewhere; I read through a lot of it and much of the code and couldn’t find anything. Consider something like:
public class Holder {
public Int64 Foo { get; set; }
}
var json = @"{""Foo"": ""a string instead of a number""}";
JsonSerializer.Deserialize<Holder>(json, options);
This throws an exception like "The JSON value could not be converted to Holder. Path: $ | LineNumber: 1 | BytePositionInLine: 32."
. While this error message isn’t useless, it could be a lot better. Ideally, it would include the actual offending field name; in this example it doesn’t matter, but in the real world our objects have a lot of (often nested) fields, and the error message will always say Path: $
.
I notice that the MemberConverter has access to the member name when it’s calling the converter, so theoretically we could hack something there to add the member name to the message. Would rather avoid hacky stuff like this, however.
What would you suggest? How can we improve our error messages? Are they supposed to be as vague as we’re getting, or is it because we’re doing something wrong?
Thanks!
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
For anyone else who has a similar issue, this is something I hacked together last night that will give error messages like
path.to.bad.field: Cannot get the value of a token type 'String' as a number.
:fix in #59