diff --git a/bson/json_util.py b/bson/json_util.py index 2862b439e..c1700d791 100644 --- a/bson/json_util.py +++ b/bson/json_util.py @@ -89,6 +89,7 @@ except ImportError: from bson import EPOCH_AWARE, RE_TYPE, SON from bson.binary import Binary +from bson.bsonint64 import BSONInt64 from bson.code import Code from bson.dbref import DBRef from bson.max_key import MaxKey @@ -193,6 +194,10 @@ def object_hook(dct, compile_re=True): return Code(dct["$code"], dct.get("$scope")) if "$uuid" in dct: return uuid.UUID(dct["$uuid"]) + if "$undefined" in dct: + return None + if "$numberLong" in dct: + return BSONInt64(dct["$numberLong"]) return dct diff --git a/test/test_json_util.py b/test/test_json_util.py index 3fefa9ff5..263fa30e3 100644 --- a/test/test_json_util.py +++ b/test/test_json_util.py @@ -22,6 +22,7 @@ import uuid sys.path[0:0] = [""] from bson import json_util +from bson.bsonint64 import BSONInt64 from bson.binary import Binary, MD5_SUBTYPE, USER_DEFINED_SUBTYPE from bson.code import Code from bson.dbref import DBRef @@ -202,6 +203,15 @@ class TestJsonUtil(unittest.TestCase): # Check order. self.assertEqual('{"$code": "return z", "$scope": {"z": 2}}', res) + def test_undefined(self): + json = '{"name": {"$undefined": true}}' + self.assertIsNone(json_util.loads(json)['name']) + + def test_numberlong(self): + json = '{"weight": {"$numberLong": 65535}}' + self.assertEqual(json_util.loads(json)['weight'], + BSONInt64(65535)) + class TestJsonUtilRoundtrip(IntegrationTest):