Skip to content
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

fix + in json #3663

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Neo.Json/JToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,6 +21,16 @@ namespace Neo.Json
/// </summary>
public abstract class JToken
{
public readonly static JavaScriptEncoder DefaultEncoder;

static JToken()
{
var textSettings = new TextEncoderSettings(UnicodeRanges.BasicLatin);
textSettings.AllowCharacter('+');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@neo-project/core Anyone can help me why is not working unless I use unsafe relaxing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JavaScriptEncoder encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping;

OR

[Fact]
        public void Ctor_WithTextEncoderSettings()
        {
            // Arrange
            var filter = new TextEncoderSettings();
            filter.AllowCharacters('a', 'b');
            filter.AllowCharacters('\0', '&', '\uFFFF', 'd');

            JavaScriptEncoder encoder = JavaScriptEncoder.Create(filter);

            // Act & assert
            Assert.Equal("a", encoder.Encode("a"));
            Assert.Equal("b", encoder.Encode("b"));
            Assert.Equal(@"\u0063", encoder.Encode("c"));
            Assert.Equal("d", encoder.Encode("d"));
            Assert.Equal(@"\u0000", encoder.Encode("\0")); // we still always encode control chars
            Assert.Equal(@"\u0026", encoder.Encode("&")); // we still always encode HTML-special chars
            Assert.Equal(@"\uFFFF", encoder.Encode("\uFFFF")); // we still always encode non-chars and other forbidden chars
        }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your "or" doesn't work with +


DefaultEncoder = JavaScriptEncoder.Create(textSettings);
}

/// <summary>
/// Represents a <see langword="null"/> token.
/// </summary>
Expand Down Expand Up @@ -239,6 +251,7 @@ public byte[] ToByteArray(bool indented)
using Utf8JsonWriter writer = new(ms, new JsonWriterOptions
{
Indented = indented,
Encoder = DefaultEncoder,
SkipValidation = true
});
Write(writer);
Expand Down
1 change: 1 addition & 0 deletions src/Neo/SmartContract/JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 5 additions & 2 deletions tests/Neo.UnitTests/SmartContract/UT_JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FormatException>(() => JObject.Parse(json));
Expand Down
Loading