Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ensure_ascii Dynamic with Default Set to True in JSON Serialization #168

Merged
merged 3 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 6.1.0
* 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)

## 6.0.1
* 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)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import subprocess

setup(name="singer-python",
version='6.0.1',
version='6.1.0',
description="Singer.io utility library",
author="Stitch",
classifiers=['Programming Language :: Python :: 3 :: Only'],
Expand Down
8 changes: 4 additions & 4 deletions singer/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,12 @@ def parse_message(msg):
return None


def format_message(message):
return json.dumps(message.asdict(), use_decimal=True)
def format_message(message, ensure_ascii=True):
return json.dumps(message.asdict(), use_decimal=True, ensure_ascii=ensure_ascii)


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


Expand Down
26 changes: 26 additions & 0 deletions tests/test_singer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import singer
import unittest
from unittest.mock import patch
import datetime
import dateutil
from decimal import Decimal
Expand Down Expand Up @@ -179,6 +180,31 @@ def test_parse_bulk_decs(self):
value = self.create_record(value_str)
self.assertEqual(Decimal(value_str), value)

@patch('sys.stdout')
def test_ensure_ascii_false(self, mock_stdout):
"""
Setting ensure_ascii=False will preserve special characters like é
in their original form.
"""
rec = {"name": "José"}
expected_output = '{"type": "RECORD", "stream": "test_stream", "record": {"name": "José"}}\n'
rec_message = singer.RecordMessage(stream="test_stream", record=rec)
result = singer.write_message(rec_message, ensure_ascii=False)
mock_stdout.write.assert_called_once_with(expected_output)
mock_stdout.flush.assert_called_once()

@patch('sys.stdout')
def test_ensure_ascii_true(self, mock_stdout):
"""
ensure_ascii defaults to True, special characters like é are
escaped into their ASCII representation (e.g., \u00e9)
"""
rec = {"name": "José"}
expected_output = '{"type": "RECORD", "stream": "test_stream", "record": {"name": "Jos\\u00e9"}}\n'
rec_message = singer.RecordMessage(stream="test_stream", record=rec)
result = singer.write_message(rec_message)
mock_stdout.write.assert_called_once_with(expected_output)
mock_stdout.flush.assert_called_once()

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