Skip to content

Commit

Permalink
Merge pull request #379 from munrojm/json_fallback
Browse files Browse the repository at this point in the history
Force `MontyDecoder` to fall back to python JSON decoder when `orjson` errors
  • Loading branch information
shyuep authored Apr 26, 2022
2 parents 618d6ff + 8f59532 commit 6d37fda
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ repos:
args: ["--profile", "black"]

- repo: https://github.com/psf/black
rev: 22.1.0
rev: 22.3.0
hooks:
- id: black

Expand Down
11 changes: 9 additions & 2 deletions monty/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,10 @@ def decode(self, s):
:return: Object.
"""
if orjson is not None:
d = orjson.loads(s) # pylint: disable=E1101
try:
d = orjson.loads(s) # pylint: disable=E1101
except orjson.JSONDecodeError: # pylint: disable=E1101
d = json.loads(s)
else:
d = json.loads(s)
return self.process_decoded(d)
Expand Down Expand Up @@ -501,7 +504,11 @@ def jsanitize(obj, strict=False, allow_bson=False, enum_values=False, recursive_
if isinstance(obj, dict):
return {
k.__str__(): jsanitize(
v, strict=strict, allow_bson=allow_bson, enum_values=enum_values, recursive_msonable=recursive_msonable
v,
strict=strict,
allow_bson=allow_bson,
enum_values=enum_values,
recursive_msonable=recursive_msonable,
)
for k, v in obj.items()
}
Expand Down
2 changes: 1 addition & 1 deletion requirements-ci.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pydocstyle
pydantic==1.9.0
flake8
pandas==1.4.1
black
black>=22.3.0
pylint
orjson==3.6.8
types-orjson==3.6.2
Expand Down
11 changes: 10 additions & 1 deletion tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,12 @@ def test_uuid(self):
d = json.loads(jsonstr, cls=MontyDecoder)
self.assertEqual(type(d["uuid"]), UUID)

def test_nan(self):
x = [float("NaN")]
djson = json.dumps(x, cls=MontyEncoder)
d = json.loads(djson)
self.assertEqual(type(d[0]), float)

def test_numpy(self):
x = np.array([1, 2, 3], dtype="int64")
self.assertRaises(TypeError, json.dumps, x)
Expand Down Expand Up @@ -357,7 +363,10 @@ def test_numpy(self):

self.assertEqual(d["np_a"]["a"][0]["b"]["@module"], "numpy")
self.assertEqual(d["np_a"]["a"][0]["b"]["@class"], "array")
self.assertEqual(d["np_a"]["a"][0]["b"]["data"], [[[1.0, 2.0], [3.0, 4.0]], [[1.0, 1.0], [1.0, 1.0]]])
self.assertEqual(
d["np_a"]["a"][0]["b"]["data"],
[[[1.0, 2.0], [3.0, 4.0]], [[1.0, 1.0], [1.0, 1.0]]],
)
self.assertEqual(d["np_a"]["a"][0]["b"]["dtype"], "complex64")

obj = ClassContainingNumpyArray.from_dict(d)
Expand Down

0 comments on commit 6d37fda

Please sign in to comment.