Skip to content

Commit 106f690

Browse files
committed
Fix json serialization of unpaired surrogates
1 parent dc9169f commit 106f690

File tree

2 files changed

+12
-7
lines changed
  • graalpython
    • com.oracle.graal.python.test/src/tests
    • com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/json

2 files changed

+12
-7
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_json.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -37,6 +37,8 @@
3737
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
# SOFTWARE.
3939

40+
import json
41+
import os
4042
import unittest
4143

4244
BIGINT_JSON_DATA = '''
@@ -58,16 +60,13 @@
5860

5961
class JsonTest(unittest.TestCase):
6062
def test_dump(self):
61-
import json
62-
import os
6363
cwd = os.getcwd()
6464
new_file_path = os.path.join(cwd, 'myFile.json')
6565
json.dump(['a', 'b', 'c'], open(new_file_path, 'w'))
6666
assert json.load(open(new_file_path)) == ['a', 'b', 'c']
6767
os.remove(new_file_path)
6868

69-
def test_load_bigin(self):
70-
import json
69+
def test_load_bigint(self):
7170
data = json.loads(BIGINT_JSON_DATA)
7271
assert "int_values" in data
7372
int_values_ = data['int_values']
@@ -83,3 +82,9 @@ def test_load_bigin(self):
8382
1521583201347000000,
8483
10,
8584
}
85+
86+
def test_encode_surrogate(self):
87+
s = json.dumps({'foo': "\uda6a"})
88+
assert s == '{"foo": "\\uda6a"}'
89+
s = json.dumps({'foo': "\uda6a"}, ensure_ascii=False)
90+
assert s == '{"foo": "\uda6a"}'

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/json/JSONUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -102,7 +102,7 @@ static void appendStringUncached(TruffleString ts, TruffleStringBuilderUTF32 bui
102102
appendEscapedUtf16Uncached((char) (0xDC00 + ((c - 0x10000) & 0x3FF)), builder);
103103
}
104104
} else {
105-
builder.appendCodePointUncached(c);
105+
builder.appendCodePointUncached(c, 1, true);
106106
}
107107
break;
108108
}

0 commit comments

Comments
 (0)