-
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
Add Utf8JsonReader.ValueIsEscaped
property
#45167
Comments
This could be done. We'd like to see concrete use-cases e.g. an app or library which is blocked on this functionality in order to prioritize something like this. |
We have this case here This is not a blocker, just an enhancement. |
One use case might be authoring zero-allocation converters from JSON string values, like some of the ones we have inbox. However, that would require also potentially exposing the unescape helpers. @ycrumeyrolle perhaps instead the API proposal in #54410 could serve to address your use case? |
Not really because I try to avoid to parse the data when not required. By using a zero-allocation converter, unescaping operation would take some time and I will not use the result except the length. |
I'd support exposing the property as proposed. @ahsonkhan @layomia @krwq any objections? |
|
I tend to agree, but we're already exposing the raw escaped data directly via |
namespace System.Text.Json
{
public partial ref struct Utf8JsonReader
{
public bool ValueIsEscaped { get; }
}
} |
Nope. The conclusion looks good, based on the naming and behavioral discussion :) |
.... Although the name @terrajobst Is that worth word-smithing some more? Is something like |
The use case is somewhat esoteric so I wouldn't object to making it longer if it does improve clarity. |
@ycrumeyrolle are you interested in adding the new API + tests? |
Keep in mind that clarity in naming has a trade-off: the more specific you are in describing the operation, the more you constrain yourself in what you can do moving forward without violating the promise the name is making. I don't have a good intuition here, but I generally agree with @eiriktsarpalis' point regarding this being advanced enough that making the name longer is generally not a bad thing. |
Ok! |
If we want it to be a bit smaller, with the same semantic meaning, this could be better: The only downside I see naming it as If we care, and since we are open to dropping the "Is" infix for a bool property (e.g. we already have Imo, the area owners (and others) can make a call between:
@eiriktsarpalis, @terrajobst, @ycrumeyrolle thoughts between the two? |
I don't have a strong opinion on the naming. The existing naming conventions in OTOH I fail to see a huge difference between |
I think either The small concern I see with the latter, is that it might be interpreted to promise some validation/escaping on the token value, which doesn't match user expectations, and then the user might incorrectly use this property to do something like: // I have a "safe" encoder that I want to run all JSON tokens through that need to be escaped.
if (reader.ValueIsEscaped)
{
// Incorrectly skip escaping it using S.T.E.W, since apparently it is already fully escaped, and safe to send as is
// When, in reality, all this checked for, is if *some* character happened to be escaped within it with `\`.
}
else
{
string actualValue = reader.GetString(); // unescapes it
var mySafeEncoder = JavaScriptEncoder.Create(UnicodeRanges.BasicLatin);
string safeEscaped = mySafeEncoder.Encode(actualValue); // now this string is safe to send for some particular use case
} @GrabYourPitchforks do you think such a concern with the name is unfounded here? |
Still working on this? |
I should be able to do this within the next two weeks. |
_stringHasEscaping
of the Utf8JsonReader
structUtf8JsonReader.ValueIsEscaped
property
The
Utf8JsonReader
struct has an internal field_stringHasEscaping
. It is useful for knowing whether the current string value need to be unescaped or not.Proposed API
namespace System.Text.Json { public ref struct Utf8JsonReader { + public bool StringHasEscaping => _stringHasEscaping; } }
Usage Examples
This might not be useful for standard usages of the
Utf8JsonReader
.But exposing this field would avoid for low-level scenarios to search for backslash with
reader.ValueSpan.IndexOf((byte)'\\') != -1
.A possible scenario is a JsonDocument-like class that that does not require all the parsing complexity, like a FlatJsonDocument for JSON documents with a depth of 1.
Afterward :
It is currently used in JsonSerializer.cs and in JsonDocument.cs.
Risks
No risk identified.
The text was updated successfully, but these errors were encountered: