Skip to content

Utf8JsonWriter.WriteString do not honor escape=false #28567

@ycrumeyrolle

Description

@ycrumeyrolle

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:

        [Fact]
        public void JsonWriter_UnescapedValue()
        {
            var output = new FixedSizedBufferWriter(100);
            var jsonUtf8 = new Utf8JsonWriter(output);
            jsonUtf8.WriteStringValue("jwt+secevent", false);
            jsonUtf8.Flush();

            string actualStr = Encoding.UTF8.GetString(output.Formatted);
            Assert.Equal(@"""jwt+secevent""", actualStr); 
            // actualStr == "jwt+secevent"
        }

When writing an unescaped value, the Utf8JsonWriter is not honoring the contract by escaping the value:

        [Fact]
        public void JsonWriter_UnescapedProperty()
        {
            var output = new FixedSizedBufferWriter(100);
            var jsonUtf8 = new Utf8JsonWriter(output);
            jsonUtf8.WriteStartObject();
            jsonUtf8.WriteString("unescaped", "jwt+secevent", false);
            jsonUtf8.WriteEndObject();
            jsonUtf8.Flush();

            string actualStr = Encoding.UTF8.GetString(output.Formatted);
            Assert.Equal(@"{""unescaped"":""jwt+secevent""}", actualStr); 
            // actualStr == "{"unescaped":"jwt\u002bsecevent"}"
        }

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions