diff --git a/src/Neo.Json/JToken.cs b/src/Neo.Json/JToken.cs index a01beba974..d80ddfea21 100644 --- a/src/Neo.Json/JToken.cs +++ b/src/Neo.Json/JToken.cs @@ -9,7 +9,9 @@ // Redistribution and use in source and binary forms with or without // modifications are permitted. +using System.Text.Encodings.Web; using System.Text.Json; +using System.Text.Unicode; using static Neo.Json.Utility; namespace Neo.Json @@ -19,6 +21,16 @@ namespace Neo.Json /// public abstract class JToken { + public readonly static JavaScriptEncoder DefaultEncoder; + + static JToken() + { + var textSettings = new TextEncoderSettings(UnicodeRanges.BasicLatin); + textSettings.AllowCharacter('+'); + + DefaultEncoder = JavaScriptEncoder.Create(textSettings); + } + /// /// Represents a token. /// @@ -239,6 +251,7 @@ public byte[] ToByteArray(bool indented) using Utf8JsonWriter writer = new(ms, new JsonWriterOptions { Indented = indented, + Encoder = DefaultEncoder, SkipValidation = true }); Write(writer); diff --git a/src/Neo/SmartContract/JsonSerializer.cs b/src/Neo/SmartContract/JsonSerializer.cs index d2055be7d8..a103978da3 100644 --- a/src/Neo/SmartContract/JsonSerializer.cs +++ b/src/Neo/SmartContract/JsonSerializer.cs @@ -96,6 +96,7 @@ public static byte[] SerializeToByteArray(StackItem item, uint maxSize) using Utf8JsonWriter writer = new(ms, new JsonWriterOptions { Indented = false, + Encoder = JToken.DefaultEncoder, SkipValidation = false }); Stack stack = new(); diff --git a/tests/Neo.UnitTests/SmartContract/UT_JsonSerializer.cs b/tests/Neo.UnitTests/SmartContract/UT_JsonSerializer.cs index fc29c95c12..de9daf3374 100644 --- a/tests/Neo.UnitTests/SmartContract/UT_JsonSerializer.cs +++ b/tests/Neo.UnitTests/SmartContract/UT_JsonSerializer.cs @@ -175,10 +175,13 @@ public void JsonTest_Object() Assert.AreEqual(@"{""test"":true}", parsed.ToString()); - json = @" {""\uAAAA"": true}"; + json = @" {""test"":""+""} "; parsed = JObject.Parse(json); + Assert.AreEqual(@"{""test"":""+""}", parsed.ToString()); - Assert.AreEqual(@"{""\uAAAA"":true}", parsed.ToString()); + json = @" {""测试"": true}"; + parsed = JObject.Parse(json); + Assert.AreEqual(@"{""测试"":true}", parsed.ToString()); json = @"{""a"":}"; Assert.ThrowsException(() => JObject.Parse(json));