Open cmeeren opened 4 years ago
Ah, this is bad indeed. It looks like the path is handled by passing a string to JsonException
, so this shouldn't be too difficult to fix.
So, as it turns out, this is not currently possible. Even if I catch the exception and raise a new one with a correct path, the exception gets mutated by System.Text.Json and its path is reset to "$" based on some internal properties of the reader. There is a proposed API to make such things extensible, and it seems that they want to have it for .NET 5, but not before.
Just to be clear, are you saying that with System.Text.Json it's not possible at all to get correct paths when implementing JSON converters for objects? (Except perhaps a hacky solution involving thread local storage or something, yuck...)
As far as I can tell from reading the source of S.T.J, that's right.
I have noticed that it's possible at least get the correct line number and byte position.
In terms of the code in this repo, instead of calling Deserialize
here:
Instead, call reader.Read()
first, and then do this:
(options.GetConverter(p.Type) :?> JsonConverter<??>).Read(&reader, p.Type, options)
I'm not sure what you should place instead of ??
since you don't have the type statically, but I'm sure you can find a reflection-based way to call Read
.
When deserialization fails, you get a message like this:
System.Text.Json.JsonException: The JSON value could not be converted to Program+C. Path: $ | LineNumber: 4 | BytePositionInLine: 20.
---> System.InvalidOperationException: Cannot get the value of a token type 'String' as a number.
The path is still incorrect, but the message is otherwise well formed, and the line number and byte position is correct and can be retrieved from the caught JsonException
object, which is very useful information for API clients.
Actually, the LineNumber
and BytePositionInLine
properties of the JsonException
are still incorrect, but at least the message is correct.
https://github.com/dotnet/runtime/issues/1562 seems to be implemented, any chance of looking into this again @tarmil? I could also have a look, but not sure if I am able to.
FSharp.SystemTextJson completely messes up the path and error messages when deserialization fails.
This is causing me some consternation, because I am creating an F# framework for JSON:API (using an immutable record-based document model) and want to return helpful deserialization messages to API clients, but that's not currently possible.
Repro:
DeserializationErrorTest.zip
Expected outcome:
Actual outcome:
Expected outcome: