From 6c81b94ee18b75a8d905770081f0dfd89e7999b5 Mon Sep 17 00:00:00 2001 From: mdsakalu Date: Tue, 11 Oct 2022 11:44:26 -0400 Subject: [PATCH 1/2] Update simplejson to 3.17.6 Fixes #62 --- requirements/base.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements/base.txt b/requirements/base.txt index 614d435..515470b 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -1 +1 @@ -simplejson==3.17.2 +simplejson==3.17.6 From 44300b7db83a675737dcd3331ef4b5232f76bd63 Mon Sep 17 00:00:00 2001 From: mdsakalu Date: Tue, 11 Oct 2022 11:46:30 -0400 Subject: [PATCH 2/2] Use unicode chars instead of escape sequences in json encoder Fixes #87 --- awslambdaric/lambda_runtime_marshaller.py | 3 ++- tests/test_lambda_runtime_marshaller.py | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/awslambdaric/lambda_runtime_marshaller.py b/awslambdaric/lambda_runtime_marshaller.py index 7eee25d..b591e69 100644 --- a/awslambdaric/lambda_runtime_marshaller.py +++ b/awslambdaric/lambda_runtime_marshaller.py @@ -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): diff --git a/tests/test_lambda_runtime_marshaller.py b/tests/test_lambda_runtime_marshaller.py index 8268de1..eeb2715 100644 --- a/tests/test_lambda_runtime_marshaller.py +++ b/tests/test_lambda_runtime_marshaller.py @@ -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