Skip to content

Commit 35c7445

Browse files
author
Luke Lovett
committed
PYTHON-708 Support $undefined and $numberLong extended JSON types.
1 parent 5136bb7 commit 35c7445

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

bson/json_util.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989

9090
from bson import EPOCH_AWARE, RE_TYPE, SON
9191
from bson.binary import Binary
92+
from bson.bsonint64 import BSONInt64
9293
from bson.code import Code
9394
from bson.dbref import DBRef
9495
from bson.max_key import MaxKey
@@ -193,6 +194,10 @@ def object_hook(dct, compile_re=True):
193194
return Code(dct["$code"], dct.get("$scope"))
194195
if "$uuid" in dct:
195196
return uuid.UUID(dct["$uuid"])
197+
if "$undefined" in dct:
198+
return None
199+
if "$numberLong" in dct:
200+
return BSONInt64(dct["$numberLong"])
196201
return dct
197202

198203

test/test_json_util.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
sys.path[0:0] = [""]
2323

2424
from bson import json_util
25+
from bson.bsonint64 import BSONInt64
2526
from bson.binary import Binary, MD5_SUBTYPE, USER_DEFINED_SUBTYPE
2627
from bson.code import Code
2728
from bson.dbref import DBRef
@@ -202,6 +203,15 @@ def test_code(self):
202203
# Check order.
203204
self.assertEqual('{"$code": "return z", "$scope": {"z": 2}}', res)
204205

206+
def test_undefined(self):
207+
json = '{"name": {"$undefined": true}}'
208+
self.assertIsNone(json_util.loads(json)['name'])
209+
210+
def test_numberlong(self):
211+
json = '{"weight": {"$numberLong": 65535}}'
212+
self.assertEqual(json_util.loads(json)['weight'],
213+
BSONInt64(65535))
214+
205215

206216
class TestJsonUtilRoundtrip(IntegrationTest):
207217

0 commit comments

Comments
 (0)