Skip to content

Commit

Permalink
Change RowAsDictJsonCoder to not ensure ASCII while encoding
Browse files Browse the repository at this point in the history
Signed-off-by: Seunghwan Hong <[email protected]>
  • Loading branch information
harrydrippin committed Jul 17, 2022
1 parent 20274f3 commit c390899
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion sdks/python/apache_beam/io/gcp/bigquery_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1537,7 +1537,7 @@ def encode(self, table_row):
# to the programmer that they have used NAN/INF values.
try:
return json.dumps(
table_row, allow_nan=False, default=default_encoder).encode('utf-8')
table_row, allow_nan=False, ensure_ascii=False, default=default_encoder).encode('utf-8')
except ValueError as e:
raise ValueError(
'%s. %s. Row: %r' % (e, JSON_COMPLIANCE_ERROR, table_row))
Expand Down
7 changes: 7 additions & 0 deletions sdks/python/apache_beam/io/gcp/bigquery_tools_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,13 @@ def test_invalid_json_inf(self):
def test_invalid_json_neg_inf(self):
self.json_compliance_exception(float('-inf'))

def test_ensure_ascii(self):
coder = RowAsDictJsonCoder()
test_value = {'s': '🎉'}
should_be = b'{"s": "\xf0\x9f\x8e\x89"}'

output_value = coder.encode(test_value)
self.assertEqual(output_value, should_be)

@unittest.skipIf(HttpError is None, 'GCP dependencies are not installed')
class TestJsonRowWriter(unittest.TestCase):
Expand Down

0 comments on commit c390899

Please sign in to comment.