PYTHON-708 Support $undefined and $numberLong extended JSON types.

This commit is contained in:
Bernie Hackett 2014-09-17 13:33:19 -07:00
parent 1ac607c447
commit 29c885311f
2 changed files with 14 additions and 0 deletions

View File

@ -193,6 +193,12 @@ def object_hook(dct, compile_re=True):
return Code(dct["$code"], dct.get("$scope"))
if bson.has_uuid() and "$uuid" in dct:
return bson.uuid.UUID(dct["$uuid"])
if "$undefined" in dct:
return None
if "$numberLong" in dct:
# 2to3 will change this to int. PyMongo 3.0 supports
# a new type, Int64, to avoid round trip issues.
return long(dct["$numberLong"])
return dct

View File

@ -221,6 +221,14 @@ class TestJsonUtil(unittest.TestCase):
# Check order.
self.assertEqual('{"$code": "return z", "$scope": {"z": 2}}', res)
def test_undefined(self):
json = '{"name": {"$undefined": true}}'
self.assertEqual(json_util.loads(json)['name'], None)
def test_numberlong(self):
json = '{"weight": {"$numberLong": 65535}}'
self.assertEqual(json_util.loads(json)['weight'], long(65535))
def test_cursor(self):
skip_restricted_localhost()
db = self.db