-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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: Path Information in JsonException.Message
#51540
Comments
Tagging subscribers to this area: @eiriktsarpalis, @layomia Issue DetailsWhen multiple (de)serializers exist and one provides converter to another, in order to have correct path information in public override string Message
{
get
{
string message = _message ?? base.Message;
if (!string.IsNullOrEmpty(Path))
{
return LineNumber.HasValue
? $"{message} | Path: {Path} | LineNumber: {LineNumber} | BytePositionInLine: {BytePositionInLine}"
: $"{message} | Path: {Path}";
}
return message;
}
}
|
Hi @weifenluo, I'm not sure I understand the problem. Would it be possible to share a minimal reproduction highlighting the issue and explaining why changing your exception message is necessary? |
I'm having a custom type I've implemented a custom serializer/deserializer for this type, using low level [JsonConverter(typeof(EntitySetJsonConverterFactory))]
public class EntitySet<T>
{
...
} The implementation of To (de)serialize
public class MyClass
{
public EntitySet<T> Data { get; set; }
} Please keep in mind The exception handling can be a little bit tricky here. Since However for use case No.2, the exception will be caught again by What I proposed here together with #51537 can solve this problem, and IMO manipulating |
So if I understand the problem correctly, you are using custom serialization logic that throws its own A duplicate of #51537 then? |
Yes, your understand is correct. It's not a duplicate of #51537, which is about |
Could you provide a minimal reproduction demonstrating the inconsistency? A console app showing the actual message versus the expected message? |
I've made a test project here: I managed to get it work as expected. The trick is when dealing with non top level object, I should rethrow a This, however, relies on internal implementation of STJ and is a bit hacky. IMO, |
When multiple (de)serializers exist and one provides converter to another, in order to have correct path information in
JsonException.Message
, we can removeJsonException.SetMessage
method andJsonException.AppendPathInformation
property, and replaceMessage
property implementation as following:The text was updated successfully, but these errors were encountered: