Skip to content

Commit

Permalink
fix: unescape unicode when json encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
yndu13 committed Oct 26, 2023
1 parent 579e973 commit b6aedb3
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 7 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/testPython.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ defaults:
working-directory: python
jobs:
build:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
strategy:
matrix:
python-version: [3.6, 3.7, 3.8, 3.9]
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11"]
fail-fast: false

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
Expand Down
10 changes: 10 additions & 0 deletions csharp/tests/CommonTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ public void Test_ToJSONString()
Assert.Equal("1", Common.ToJSONString(1));
Assert.Equal("true", Common.ToJSONString(true));
Assert.Equal("null", Common.ToJSONString(null));
Dictionary<string, object> unicode = new Dictionary<string, object>
{ { "str", "test&<>://中文" }
};
Assert.Equal("{\"key\":\"value\",\"map\":{\"str\":\"test&<>://中文\"},\"num\":1}", Common.ToJSONString(
new Dictionary<string, object>
{
{ "key", "value" },
{ "map", unicode },
{ "num", 1 }
}));
}

[Fact]
Expand Down
8 changes: 5 additions & 3 deletions golang/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,13 @@ func ToJSONString(a interface{}) *string {
}
return tea.String(string(byt))
}
byt, err := json.Marshal(a)
if err != nil {
byt := bytes.NewBuffer([]byte{})
jsonEncoder := json.NewEncoder(byt)
jsonEncoder.SetEscapeHTML(false)
if err := jsonEncoder.Encode(a); err != nil {
return nil
}
return tea.String(string(byt))
return tea.String(string(bytes.TrimSpace(byt.Bytes())))
}

func DefaultNumber(reaNum, defaultNum *int) *int {
Expand Down
4 changes: 4 additions & 0 deletions golang/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,4 +445,8 @@ func TestToJSONString(t *testing.T) {
utils.AssertEqual(t, "abcd", tea.StringValue(str))
str = ToJSONString(tests[2])
utils.AssertEqual(t, "hello", tea.StringValue(str))

testMap := map[string]interface{}{"key": "value", "map": map[string]string{"str": "test&<>://中文"}, "num": 1}
str = ToJSONString(testMap)
utils.AssertEqual(t, "{\"key\":\"value\",\"map\":{\"str\":\"test&<>://中文\"},\"num\":1}", tea.StringValue(str))
}
2 changes: 2 additions & 0 deletions swift/Tests/TeaUtilsTests/TeaUtilsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,15 @@ final class ClientTests: XCTestCase {
map["key1"] = "value1"
map["key2"] = 2
map["key3"] = true
map["str"] = "test&<>://中文"
dict["map"] = map

XCTAssertTrue(Client.toJSONString(dict).contains("\"foo\":\"bar\""))
XCTAssertTrue(Client.toJSONString(dict).contains("\"bool\":true"))
XCTAssertTrue(Client.toJSONString(dict).contains("\"key1\":\"value1\""))
XCTAssertTrue(Client.toJSONString(dict).contains("\"key2\":2"))
XCTAssertTrue(Client.toJSONString(dict).contains("\"key3\":true"))
XCTAssertTrue(Client.toJSONString(dict).contains("\"str\":\"test&<>:\\/\\/中文\""))
XCTAssertTrue(Client.toJSONString(dict).contains("\"num\":10"))
XCTAssertTrue(Client.toJSONString(dict).contains("\"list\":[\"1\",2,false]"))

Expand Down
1 change: 1 addition & 0 deletions ts/test/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('Tea Util', function () {
assert.deepStrictEqual(Client.toJSONString(1), '1');
assert.deepStrictEqual(Client.toJSONString(true), 'true');
assert.deepStrictEqual(Client.toJSONString(null), 'null');
assert.deepStrictEqual(Client.toJSONString({ 'str': 'test', 'number': 1, 'bool': false, 'null': null }), '{"str":"test","number":1,"bool":false,"null":null}');
});

it('defaultString should ok', function () {
Expand Down

0 comments on commit b6aedb3

Please sign in to comment.