-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Utf8JsonWriter.WriteString do not honor escape=false #28567
Comments
I haven't seen the comment: |
In addition, I do not understand the reason that the '+' is escaped. Same remark for '&', '<', '>' and '`'. |
Right. This is by design. Usually, property names are known/constant values, so the
That's a good point. I don't know if there is a strong use case for writing unescaped string arrays, so for consistency, we should consider removing the escape argument on
These are common Html characters that need to be escaped since JSON can end up embedded within html web pages. |
Seems like the parameter should be renamed then to avoid any ambiguity? e.g. escape => escapeName |
Can't we imagine a new property on the public void WriteString(string propertyName, string value, bool escapeName = true, bool escapeValue = true);
public void WriteString(string propertyName, ReadOnlySpan<char> value, bool escapeName = true, bool escapeValue = true);
public void WriteString(string propertyName, ReadOnlySpan<byte> utf8Value, bool escapeName = true, bool escapeValue = true);
public void WriteString(ReadOnlySpan<char> propertyName, string value, bool escapeName = true, bool escapeValue = true);
public void WriteString(ReadOnlySpan<char> propertyName, ReadOnlySpan<char> value, bool escapeName = true, bool escapeValue = true);
public void WriteString(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> utf8Value, bool escapeName = true, bool escapeValue = true);
public void WriteString(ReadOnlySpan<byte> utf8PropertyName, string value, bool escapeName = true, bool escapeValue = true);
public void WriteString(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<char> value, bool escapeName = true, bool escapeValue = true);
public void WriteString(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> utf8Value, bool escapeName = true, bool escapeValue = true);
The security consideration is no clear to me. |
I just tried Utf8JsonWriter.WriteString(name, value, false), then Original value = "春夏秋冬" (12bytes) Is this by design? Too long and unreadable. |
Closing this as https://github.com/dotnet/corefx/issues/37192 addresses this by passing a custom escaper to the writer. |
it work!!! object jsonObject = new { symbol = @"~`!@#$%^&*()_-+={}[]:;'<>,.?/ " };
string aJsonString = Newtonsoft.Json.JsonConvert.SerializeObject(value: jsonObject);
string bJsonString = System.Text.Json.JsonSerializer.Serialize(
value: jsonObject,
options: new System.Text.Json.JsonSerializerOptions
{
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
});
Assert.AreEqual(expected: aJsonString, actual: bJsonString); |
When writing an unescaped value like "jwt+secevent", the
Utf8JsonWriter
is honoring the contract by not escaping the value, as illustrated in the following test:When writing an unescaped value, the
Utf8JsonWriter
is not honoring the contract by escaping the value:In this test, the '+' character is escaped to '\u002b'.
The expected behavior is to have an unescaped string when it is a JSON value and a value of a JSON property.
The text was updated successfully, but these errors were encountered: