@@ -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