Consider adding JsonDocument.TryParse()
Background and motivation
Hey,
since the „ https://github.com/dotnet/runtime/issues/387“ issue was closed, but
- i work with the System.Text.Json API a lot
- i still think @ADustyOldMuffin is totally right here: https://github.com/dotnet/runtime/issues/387#issuecomment-717645278
- i can no longer upvote for this
- i have my 2 cents i want to give as feedback
i decided to open this issue.
As @ADustyOldMuffin said: We can safely assume that at least 80% of all ppls would assume a false TryParse() means „no valid JSON format“.
But what nobody discussed, to what some exception-based Parse() concept leads and what must be handled with then.
Since JsonDocument is IDisposable and since you needd to pack it into a try/catch block, to return a general „invalid JSON“-like error, this end up in some clunky handling. Even more when async/await and Tasks come into play. I had a hard time, debugging stupid asynchronous behaviours and disposing stuff that ended up existing, solely for the reason that less experienced devs put JsonDocument.Parse into a try/catch block and start creating some weird bug-prone code concepts.
Long story short: There are more good reasons to have some non try/catch based JSON parsing, besides the discussion about, what behavior ppls expect from TryXXX() API guidelines.
Best regards
API Proposal
if (!JsonDocument.TryParse(jsonString, out JsonDocument jsonDocument))
{
//stuff
}
additional to
try
{
JsonDocument.Parse(jsonString);
}
catch
{
//stuff
}
API Usage
See above
Alternative Designs
No response
Risks
- Less risk, when it comes down to disposing.
- More risk that there are maybe 20% ppls, which not understand a false return from TryParse() as „invalid JSON format“.
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis See info in area-owners.md if you want to be subscribed.
It's not something we'd be able to support for the reasons outlined in the original issue: there are too many failure modes and the underlying Utf8JsonReader that drives the parsing does support it. My recommendation would be to write a helper that catches any exceptions and returns false.
It's not something we'd be able to support for the reasons outlined in the original issue: there are too many failure modes and the underlying
Utf8JsonReaderthat drives the parsing does support it. My recommendation would be to write a helper that catches any exceptions and returns false.
Which is exactly, what MS could deliver with the API, as i.e. an extension method.
But OK, i see, the creators have a different point of view, at these things, than we, as just the users of those stuff. Which is perfectly fine. At least it was worth the try and i had a chance of giving feedback. And that’s fair enough.
Which is exactly, what MS could deliver with the API, as i.e. an extension method.
As mentioned, this is not something we would consider because it goes against our design guidelines when it comes to authoring Try- methods:
When using this pattern, it is important to define the try functionality in strict terms. If the member fails for any reason other than the well-defined try, the member must still throw a corresponding exception.
This should not prevent anyone interested from authoring an extension method like that.