runtime: JsonException is too general
While working on migrating PowerShell ConvertTo-Json cmdlet from Newtonsoft to new Core Json API I discovered that JsonException is too general to be finely processed.
For example, Json API detects a cycle, throw with JsonException and it is impossible to determine what exactly this event happened. (Only message contains the information but it is localized). We can see more examples in the file https://github.com/dotnet/runtime/blob/master/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Serialization.cs
Proposal: Json API should throw with more specific exceptions derived from JsonException class like Newtonsoft Json.NET does.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 2
- Comments: 17 (12 by maintainers)
Huh. I get “There is not enough data to read through the entire JSON array or object.”, which the one named
NotEnoughData. What I meant with my last message, is that the exception messages themselves are already quite detailed, meaning some thought has been dedicated to what exactly has “gone wrong” in each case, and therefore could be used to create a list of all the possible errors. However, this example shows that they might be too detailed, and need to be grouped up into types.The exception case that I’m looking for is
NotEnoughData(as the current message is named inSystem.Text.Json/src/Resources/Strings.resx): I want to distinguish between when deserialization fails because some data was missing (meaning I need to perform IO to receive more data), and when it fails because of an actual error in the JSON (meaning I should report error and abort).Alternatively, introduce an error code enum.