Skip to content

Commit 0cb2288

Browse files
authored
Make ensure_ascii Dynamic with Default Set to True in JSON Serialization (#168)
* add parameter - ensure_ascii to load non ascii characters when set to false * add unit test for ensuring ascii characters while loading * update setup and changelog
1 parent d6f0d20 commit 0cb2288

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## 6.1.0
4+
* Make ensure_ascii Dynamic with Default Set to True in JSON Serialization. Required to handle the special characters [#168](https://github.com/singer-io/singer-python/pull/168)
5+
36
## 6.0.1
47
* Pin backoff and simplejson to any version greater than or equal to the previously allowed version, up to the next major version [#167](https://github.com/singer-io/singer-python/pull/167)
58

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import subprocess
55

66
setup(name="singer-python",
7-
version='6.0.1',
7+
version='6.1.0',
88
description="Singer.io utility library",
99
author="Stitch",
1010
classifiers=['Programming Language :: Python :: 3 :: Only'],

singer/messages.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,12 @@ def parse_message(msg):
218218
return None
219219

220220

221-
def format_message(message):
222-
return json.dumps(message.asdict(), use_decimal=True)
221+
def format_message(message, ensure_ascii=True):
222+
return json.dumps(message.asdict(), use_decimal=True, ensure_ascii=ensure_ascii)
223223

224224

225-
def write_message(message):
226-
sys.stdout.write(format_message(message) + '\n')
225+
def write_message(message, ensure_ascii=True):
226+
sys.stdout.write(format_message(message, ensure_ascii=ensure_ascii) + '\n')
227227
sys.stdout.flush()
228228

229229

tests/test_singer.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import singer
22
import unittest
3+
from unittest.mock import patch
34
import datetime
45
import dateutil
56
from decimal import Decimal
@@ -179,6 +180,31 @@ def test_parse_bulk_decs(self):
179180
value = self.create_record(value_str)
180181
self.assertEqual(Decimal(value_str), value)
181182

183+
@patch('sys.stdout')
184+
def test_ensure_ascii_false(self, mock_stdout):
185+
"""
186+
Setting ensure_ascii=False will preserve special characters like é
187+
in their original form.
188+
"""
189+
rec = {"name": "José"}
190+
expected_output = '{"type": "RECORD", "stream": "test_stream", "record": {"name": "José"}}\n'
191+
rec_message = singer.RecordMessage(stream="test_stream", record=rec)
192+
result = singer.write_message(rec_message, ensure_ascii=False)
193+
mock_stdout.write.assert_called_once_with(expected_output)
194+
mock_stdout.flush.assert_called_once()
195+
196+
@patch('sys.stdout')
197+
def test_ensure_ascii_true(self, mock_stdout):
198+
"""
199+
ensure_ascii defaults to True, special characters like é are
200+
escaped into their ASCII representation (e.g., \u00e9)
201+
"""
202+
rec = {"name": "José"}
203+
expected_output = '{"type": "RECORD", "stream": "test_stream", "record": {"name": "Jos\\u00e9"}}\n'
204+
rec_message = singer.RecordMessage(stream="test_stream", record=rec)
205+
result = singer.write_message(rec_message)
206+
mock_stdout.write.assert_called_once_with(expected_output)
207+
mock_stdout.flush.assert_called_once()
182208

183209
if __name__ == '__main__':
184210
unittest.main()

0 commit comments

Comments
 (0)