Skip to content

Commit 4fedef5

Browse files
CopilotAndriySvyryd
andcommitted
Implement proper fix for Chinese character escaping in JSON using AppContext switch
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
1 parent cb71c34 commit 4fedef5

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/EFCore.SqlServer/Storage/Internal/SqlServerOwnedJsonTypeMapping.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,7 @@ protected virtual string EscapeSqlLiteral(string literal)
105105
/// doing so can result in application failures when updating to a new Entity Framework Core release.
106106
/// </summary>
107107
protected override string GenerateNonNullSqlLiteral(object value)
108-
{
109-
var jsonString = value is string str ? str : JsonSerializer.Serialize(value);
110-
return $"'{EscapeSqlLiteral(jsonString)}'";
111-
}
108+
=> $"'{EscapeSqlLiteral(JsonSerializer.Serialize(value))}'";
112109

113110
/// <summary>
114111
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to

src/EFCore/Storage/Json/JsonStringReaderWriter.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Text.Encodings.Web;
45
using System.Text.Json;
56

67
namespace Microsoft.EntityFrameworkCore.Storage.Json;
@@ -12,6 +13,9 @@ public sealed class JsonStringReaderWriter : JsonValueReaderWriter<string>
1213
{
1314
private static readonly PropertyInfo InstanceProperty = typeof(JsonStringReaderWriter).GetProperty(nameof(Instance))!;
1415

16+
private static readonly bool UseOldBehavior32152 =
17+
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue32152", out var enabled32152) && enabled32152;
18+
1519
/// <summary>
1620
/// The singleton instance of this stateless reader/writer.
1721
/// </summary>
@@ -27,7 +31,16 @@ public override string FromJsonTyped(ref Utf8JsonReaderManager manager, object?
2731

2832
/// <inheritdoc />
2933
public override void ToJsonTyped(Utf8JsonWriter writer, string value)
30-
=> writer.WriteStringValue(value);
34+
{
35+
if (UseOldBehavior32152)
36+
{
37+
writer.WriteStringValue(value);
38+
}
39+
else
40+
{
41+
writer.WriteStringValue(JsonEncodedText.Encode(value, JavaScriptEncoder.UnsafeRelaxedJsonEscaping));
42+
}
43+
}
3144

3245
/// <inheritdoc />
3346
public override Expression ConstructorExpression

0 commit comments

Comments
 (0)