Skip to content

Commit c7df763

Browse files
committed
https://github.com/robotframework-thailand/robotframework-jsonlibrary/issues/59
1 parent 33dcae8 commit c7df763

File tree

5 files changed

+78
-3
lines changed

5 files changed

+78
-3
lines changed

AGENT.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# Rules
22

33
- Use DRY when possible to not repeat yourself.
4+
- Run the `run_ci_checks.sh` script anytime you make changes and fix any errors or warnings that it finds.

JSONLib/jsonlib.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,22 +202,25 @@ def delete_object_from_json(self, json_object, json_path):
202202
return json_object_cpy
203203

204204
@staticmethod
205-
def convert_json_to_string(json_object, indent=None):
205+
def convert_json_to_string(json_object, indent=None, ensure_ascii=True):
206206
"""Convert JSON object to string
207207
208208
Arguments:
209209
- json_object: json as a dictionary object.
210210
- indent: indent level for pretty-printing, see indent argument of
211211
python's [https://docs.python.org/3/library/json.html#json.dump|json.dump()]
212212
for details
213+
- ensure_ascii: if True (default), escape all non-ASCII characters.
214+
If False, non-ASCII characters are preserved in the output.
213215
214216
Return new json_string
215217
216218
Examples:
217219
| ${json_str}= | Convert JSON To String | ${json_obj} |
218220
| ${json_str}= | Convert JSON To String | ${json_obj} | indent=${4} |
221+
| ${json_str}= | Convert JSON To String | ${json_obj} | indent=${4} | ensure_ascii=False |
219222
"""
220-
return json.dumps(json_object, indent=indent)
223+
return json.dumps(json_object, indent=indent, ensure_ascii=ensure_ascii)
221224

222225
@staticmethod
223226
def convert_string_to_json(json_string):

acceptance/JSONLib.robot

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,34 @@ TestConvertJSONToString
8282
${json_str}= Convert Json To String ${json_obj_input}
8383
Should Be String ${json_str}
8484

85+
TestEnsureAsciiDefault
86+
[Documentation] Test that ensure_ascii=True by default (escapes non-ASCII)
87+
${json_obj}= Create Dictionary test=über
88+
${json_str}= Convert Json To String ${json_obj}
89+
Should Contain ${json_str} \\u00fc
90+
Should Not Contain ${json_str} über
91+
92+
TestEnsureAsciiTrue
93+
[Documentation] Test that ensure_ascii=True explicitly escapes non-ASCII
94+
${json_obj}= Create Dictionary test=café greeting=你好
95+
${json_str}= Convert Json To String ${json_obj} ensure_ascii=True
96+
Should Contain ${json_str} \\u
97+
Should Not Contain ${json_str} café
98+
99+
TestEnsureAsciiFalse
100+
[Documentation] Test that ensure_ascii=False preserves UTF-8 characters
101+
${json_obj}= Create Dictionary test=über greeting=你好 coffee=café
102+
${json_str}= Convert Json To String ${json_obj} ensure_ascii=False
103+
Should Contain ${json_str} über
104+
Should Contain ${json_str} 你好
105+
Should Contain ${json_str} café
106+
107+
TestEnsureAsciiFalseWithIndent
108+
[Documentation] Test that ensure_ascii=False works with indent
109+
${json_obj}= Create Dictionary test=über
110+
${json_str}= Convert Json To String ${json_obj} indent=${4} ensure_ascii=False
111+
Should Contain ${json_str} über
112+
85113
TestDumpJSONToFile
86114
[Documentation] Dumps Json to file
87115
Dump Json to file ${TEMPDIR}/sample_dump.json ${json_obj_input}

docs/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/test_JSONLib.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,49 @@ def test_convert_json_to_string_with_indent(self, json):
158158
assert len(json_str_as_list := json_str.split("\n")) == 45
159159
assert json_str_as_list[1][:4] == " " * 4
160160

161+
def test_convert_json_to_string_ensure_ascii_default(self):
162+
"""Test that ensure_ascii=True by default (escapes non-ASCII characters)"""
163+
json_obj = {"test": "über"}
164+
json_str = self.json_library.convert_json_to_string(json_obj)
165+
# With ensure_ascii=True, ü should be escaped as \u00fc
166+
assert "\\u00fc" in json_str
167+
assert "über" not in json_str
168+
169+
def test_convert_json_to_string_ensure_ascii_true(self):
170+
"""Test that ensure_ascii=True explicitly escapes non-ASCII characters"""
171+
json_obj = {"test": "café", "greeting": "你好"}
172+
json_str = self.json_library.convert_json_to_string(json_obj, ensure_ascii=True)
173+
# With ensure_ascii=True, non-ASCII characters should be escaped
174+
assert "\\u00e9" in json_str # é
175+
assert "café" not in json_str
176+
assert "你好" not in json_str
177+
178+
def test_convert_json_to_string_ensure_ascii_false(self):
179+
"""Test that ensure_ascii=False preserves UTF-8 characters"""
180+
json_obj = {"test": "über", "greeting": "你好", "coffee": "café"}
181+
json_str = self.json_library.convert_json_to_string(
182+
json_obj, ensure_ascii=False
183+
)
184+
# With ensure_ascii=False, UTF-8 characters should be preserved
185+
assert "über" in json_str
186+
assert "你好" in json_str
187+
assert "café" in json_str
188+
# Should not have escape sequences
189+
assert "\\u00fc" not in json_str
190+
assert "\\u00e9" not in json_str
191+
192+
def test_convert_json_to_string_ensure_ascii_false_with_indent(self):
193+
"""Test that ensure_ascii=False works with indent parameter"""
194+
json_obj = {"test": "über", "nested": {"greeting": "你好"}}
195+
json_str = self.json_library.convert_json_to_string(
196+
json_obj, indent=2, ensure_ascii=False
197+
)
198+
# With ensure_ascii=False, UTF-8 characters should be preserved
199+
assert "über" in json_str
200+
assert "你好" in json_str
201+
# Should also be properly indented
202+
assert "\n" in json_str
203+
161204
def test_convert_string_to_json(self, json):
162205
json_obj = self.json_library.convert_string_to_json('{"firstName": "John"}')
163206
assert "firstName" in json_obj

0 commit comments

Comments
 (0)