From d1dc11242ee92073bfd5231aa49c0b259a35521d Mon Sep 17 00:00:00 2001 From: Ross Lawley Date: Fri, 9 Nov 2012 08:56:23 +0000 Subject: [PATCH] Recurse when dumping DBRefs PYTHON-433 --- bson/json_util.py | 2 +- test/test_json_util.py | 18 +++++------------- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/bson/json_util.py b/bson/json_util.py index d424ce396..da4d06554 100644 --- a/bson/json_util.py +++ b/bson/json_util.py @@ -163,7 +163,7 @@ def default(obj): if isinstance(obj, ObjectId): return {"$oid": str(obj)} if isinstance(obj, DBRef): - return obj.as_doc() + return _json_convert(obj.as_doc()) if isinstance(obj, datetime.datetime): # TODO share this code w/ bson.py? if obj.utcoffset() is not None: diff --git a/test/test_json_util.py b/test/test_json_util.py index a97246c86..df1cca448 100644 --- a/test/test_json_util.py +++ b/test/test_json_util.py @@ -63,18 +63,8 @@ class TestJsonUtil(unittest.TestCase): def test_dbref(self): self.round_trip({"ref": DBRef("foo", 5)}) self.round_trip({"ref": DBRef("foo", 5, "db")}) - - # TODO this is broken when using cjson. See: - # http://jira.mongodb.org/browse/PYTHON-153 - # http://bugs.python.org/issue6105 - # - # self.assertEqual("{\"ref\": {\"$ref\": \"foo\", \"$id\": 5}}", - # json.dumps({"ref": DBRef("foo", 5)}, - # default=json_util.default)) - # self.assertEqual("{\"ref\": {\"$ref\": \"foo\", - # \"$id\": 5, \"$db\": \"bar\"}}", - # json.dumps({"ref": DBRef("foo", 5, "bar")}, - # default=json_util.default)) + self.round_trip({"ref": DBRef("foo", ObjectId())}) + self.round_trip({"ref": DBRef("foo", ObjectId(), "db")}) def test_datetime(self): # only millis, not micros @@ -128,7 +118,9 @@ class TestJsonUtil(unittest.TestCase): {'foo': [1, 2]}, {'bar': {'hello': 'world'}}, {'code': Code("function x() { return 1; }")}, - {'bin': Binary(b("\x00\x01\x02\x03\x04"))} + {'bin': Binary(b("\x00\x01\x02\x03\x04"))}, + {'dbref': {'_ref': DBRef('simple', + ObjectId('509b8db456c02c5ab7e63c34'))}} ] db.test.insert(docs)