Skip to content

Commit

Permalink
Merge pull request #88 from mdsakalu/main
Browse files Browse the repository at this point in the history
Use unicode chars instead of escape sequences in json encoder output and bump simplejson version
  • Loading branch information
briensea authored Oct 24, 2023
2 parents 186003d + 44300b7 commit 8e3bb0c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
3 changes: 2 additions & 1 deletion awslambdaric/lambda_runtime_marshaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@

# simplejson's Decimal encoding allows '-NaN' as an output, which is a parse error for json.loads
# to get the good parts of Decimal support, we'll special-case NaN decimals and otherwise duplicate the encoding for decimals the same way simplejson does
# We also set 'ensure_ascii=False' so that the encoded json contains unicode characters instead of unicode escape sequences
class Encoder(json.JSONEncoder):
def __init__(self):
super().__init__(use_decimal=False)
super().__init__(use_decimal=False, ensure_ascii=False)

def default(self, obj):
if isinstance(obj, decimal.Decimal):
Expand Down
2 changes: 1 addition & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
simplejson==3.17.2
simplejson==3.17.6
6 changes: 6 additions & 0 deletions tests/test_lambda_runtime_marshaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ def test_json_serializer_is_not_default_json(self):
self.assertTrue(hasattr(internal_json, "YOLO"))
self.assertFalse(hasattr(stock_json, "YOLO"))
self.assertTrue(hasattr(simplejson, "YOLO"))

def test_to_json_unicode_encoding(self):
response = to_json({"price": "£1.00"})
self.assertEqual('{"price": "£1.00"}', response)
self.assertNotEqual('{"price": "\\u00a31.00"}', response)
self.assertEqual(19, len(response.encode('utf-8'))) # would be 23 bytes if a unicode escape was returned

0 comments on commit 8e3bb0c

Please sign in to comment.