Skip to content

Commit d0ab30b

Browse files
committed
Remove simplejson dependency
When simplejson is installed, `requests` behaves differently https://github.com/psf/requests/blob/1764cc938efc3cc9720188dfa6c3852c45211aa0/src/requests/compat.py#L59-L64 which affects custom JSONEncode/Decoders Instead of relying on simplejson for decimal support implement decimal support directly Remove the `internal_json` vs `stock_json` test comparison
1 parent f65d966 commit d0ab30b

File tree

4 files changed

+15
-49
lines changed

4 files changed

+15
-49
lines changed

Diff for: THIRD-PARTY-LICENSES

-24
Original file line numberDiff line numberDiff line change
@@ -204,30 +204,6 @@ limitations under the License.
204204

205205
------
206206

207-
** python-simplejson; version 3.17.2 --
208-
https://github.com/simplejson/simplejson
209-
Copyright (c) 2006 Bob Ippolito
210-
211-
Permission is hereby granted, free of charge, to any person obtaining a copy of
212-
this software and associated documentation files (the "Software"), to deal in
213-
the Software without restriction, including without limitation the rights to
214-
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
215-
of the Software, and to permit persons to whom the Software is furnished to do
216-
so, subject to the following conditions:
217-
218-
The above copyright notice and this permission notice shall be included in all
219-
copies or substantial portions of the Software.
220-
221-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
222-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
223-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
224-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
225-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
226-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
227-
SOFTWARE.
228-
229-
------
230-
231207
** libcurl; version 7.83.1 -- https://github.com/curl/curl
232208
Copyright (c) 1996 - 2022, Daniel Stenberg, [email protected], and many
233209
contributors, see the THANKS file.

Diff for: awslambdaric/lambda_runtime_marshaller.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,37 @@
55
import decimal
66
import math
77
import os
8-
import simplejson as json
8+
import json
99

1010
from .lambda_runtime_exception import FaultException
1111

1212

13-
# simplejson's Decimal encoding allows '-NaN' as an output, which is a parse error for json.loads
14-
# 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
15-
# We also set 'ensure_ascii=False' so that the encoded json contains unicode characters instead of unicode escape sequences
13+
# We force a serialization of a Decimal's string as raw instead of wrapped by quotes
14+
# by mocking a float, and overriding the __repr__ method to return the string
15+
class DecimalStr(float):
16+
def __init__(self, value: decimal.Decimal):
17+
self._value = value
18+
def __repr__(self):
19+
return str(self._value)
20+
1621
class Encoder(json.JSONEncoder):
1722
def __init__(self):
1823
if os.environ.get("AWS_EXECUTION_ENV") in {
1924
"AWS_Lambda_python3.12",
2025
"AWS_Lambda_python3.13",
2126
}:
22-
super().__init__(use_decimal=False, ensure_ascii=False, allow_nan=True)
27+
# We also set 'ensure_ascii=False' so that the encoded json contains unicode characters instead of unicode escape sequences
28+
super().__init__(ensure_ascii=False, allow_nan=True)
2329
else:
24-
super().__init__(use_decimal=False, allow_nan=True)
30+
super().__init__(allow_nan=True)
2531

32+
# simplejson's Decimal encoding allows '-NaN' as an output, which is a parse error for json.loads
33+
# 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
2634
def default(self, obj):
2735
if isinstance(obj, decimal.Decimal):
2836
if obj.is_nan():
2937
return math.nan
30-
return json.raw_json.RawJSON(str(obj))
38+
return DecimalStr(obj)
3139
return super().default(obj)
3240

3341

Diff for: requirements/base.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
simplejson>=3.20.1
21
snapshot-restore-py>=1.0.0

Diff for: tests/test_lambda_runtime_marshaller.py

-17
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,6 @@ def test_to_json_decimal_encoding_negative_nan(self):
4848
response = to_json({"pi": decimal.Decimal("-nan")})
4949
self.assertEqual('{"pi": NaN}', response)
5050

51-
def test_json_serializer_is_not_default_json(self):
52-
from awslambdaric.lambda_runtime_marshaller import (
53-
json as internal_json,
54-
)
55-
import simplejson as simplejson
56-
import json as stock_json
57-
import json
58-
59-
self.assertEqual(json, stock_json)
60-
self.assertNotEqual(stock_json, internal_json)
61-
self.assertNotEqual(stock_json, simplejson)
62-
63-
internal_json.YOLO = "bello"
64-
self.assertTrue(hasattr(internal_json, "YOLO"))
65-
self.assertFalse(hasattr(stock_json, "YOLO"))
66-
self.assertTrue(hasattr(simplejson, "YOLO"))
67-
6851
@parameterized.expand(execution_envs_lambda_marshaller_ensure_ascii_false)
6952
def test_to_json_unicode_not_escaped_encoding(self, execution_env):
7053
os.environ = {"AWS_EXECUTION_ENV": execution_env}

0 commit comments

Comments
 (0)