Skip to content

Commit 9466b91

Browse files
committed
chore: change JavascriptEncoder settings and add related tests
1 parent 9c13117 commit 9466b91

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/Docfx.Common/Json/System.Text.Json/SystemTextJsonUtility.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static SystemTextJsonUtility()
3333
// DefaultBufferSize = 1024 * 16, // TODO: Set appropriate buffer size based on benchmark.(Default: 16KB)
3434
AllowTrailingCommas = true,
3535
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
36-
// Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, // Some chars are escaped by this settings (e.g. `+`, ` `(full width space)
36+
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, // TODO: Replace with custom encoder that encode minimal chars (https://github.com/dotnet/runtime/issues/87153)
3737
PropertyNameCaseInsensitive = true,
3838
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
3939
// DictionaryKeyPolicy = JsonNamingPolicy.CamelCase, // This setting is not compatible to `Newtonsoft.Json` serialize result.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Docfx.Common;
5+
using FluentAssertions;
6+
7+
namespace docfx.Tests;
8+
9+
public partial class JsonSerializationEncoderTest
10+
{
11+
[Theory]
12+
[InlineData("abcdefghighlmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")]
13+
[InlineData("0123456789")]
14+
[InlineData("\0\a\b\t\n\v\f\r\e")]
15+
[InlineData("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~")]
16+
[InlineData("①②③")] // NonAscii chars (Enclosed Alphanumerics)
17+
[InlineData("123")] // NonAscii chars (Full-width digits)
18+
[InlineData("äöü")] // Umlaut
19+
[InlineData("漢字")] // Kanji
20+
public void JsonEncoderTest(string data)
21+
{
22+
// Arrange
23+
var model = data;
24+
25+
//Act
26+
var systemTextJsonResult = SystemTextJsonUtility.Serialize(model);
27+
var newtonsoftJsonResult = NewtonsoftJsonUtility.Serialize(model);
28+
29+
// Assert
30+
// Compare serialized result text with `StringComparer.OrdinalIgnoreCase`
31+
// - SystemTextJson escape chars with capital case(`\u001B`)
32+
// - NewtonsoftJson escape chars with lower case (`\u001b`)
33+
((object)systemTextJsonResult).Should().Be(newtonsoftJsonResult, StringComparer.OrdinalIgnoreCase); // Currently StringAssertions don't expose overload that accepts StringComparer. (See: https://github.com/fluentassertions/fluentassertions/issues/2720)
34+
}
35+
36+
[Theory]
37+
[InlineData(" ", @"\u3000")] // Full-Width space (Excaped by global block list (https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/character-encoding#global-block-list))
38+
[InlineData("𠮟", @"\uD842\uDF9F")] // Kanji (that use Surrogate Pair)
39+
[InlineData("📄", @"\uD83D\uDCC4")] // Emoji
40+
[InlineData("👁‍🗨", @"\uD83D\uDC41‍\uD83D\uDDE8")] // Emoji (with ZWJ (ZERO WIDTH JOINER))
41+
public void JsonEncoderTest_NoCompatibility(string data, string expected)
42+
{
43+
// Arrange
44+
var model = data;
45+
46+
//Act
47+
var systemTextJsonResult = SystemTextJsonUtility.Serialize(model);
48+
var newtonsoftJsonResult = NewtonsoftJsonUtility.Serialize(model);
49+
50+
// Assert
51+
systemTextJsonResult.Should().NotBe(newtonsoftJsonResult);
52+
53+
systemTextJsonResult.Should().Contain(expected);
54+
newtonsoftJsonResult.Should().Contain(data);
55+
}
56+
}

0 commit comments

Comments
 (0)