Skip to content

Commit 47b7324

Browse files
committed
added loads and dumps interface (to facilitate using bson as a json replacement)
1 parent 9b14954 commit 47b7324

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

bson/__init__.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,26 @@ def decode(self, as_class=dict, tz_aware=False):
509509
return document
510510

511511

512+
def dumps(document):
513+
"""Decode a bson document into its :class:`dict` representation.
514+
515+
This interface mirrors that of the json module.
516+
:Parameters:
517+
- `document`: mapping type representing a document
518+
"""
519+
return BSON.encode(document)
520+
521+
522+
def loads(bsondoc):
523+
"""Encode a :class:`dict` document into its BSON data representation.
524+
525+
This interface mirrors that of the json module.
526+
:Parameters:
527+
- `bsondoc`: string representing a bson document
528+
"""
529+
return BSON(bsondoc).decode()
530+
531+
512532
def has_c():
513533
"""Is the C extension installed?
514534

test/test_bson.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,5 +380,62 @@ def test_ordered_dict(self):
380380
d = OrderedDict([("one", 1), ("two", 2), ("three", 3), ("four", 4)])
381381
self.assertEqual(d, BSON.encode(d).decode(as_class=OrderedDict))
382382

383+
def test_loads_dumps(self):
384+
385+
def _test_conversion(dictrep, bsonrep):
386+
self.assertEqual(bson.dumps(dictrep), bsonrep)
387+
self.assertEqual(bson.loads(bsonrep), dictrep)
388+
self.assertEqual(bson.loads(bson.dumps(dictrep)), dictrep)
389+
self.assertEqual(bson.dumps(bson.loads(bsonrep)), bsonrep)
390+
391+
_test_conversion({}, "\x05\x00\x00\x00\x00")
392+
_test_conversion({"test": u"hello world"},
393+
"\x1B\x00\x00\x00\x02\x74\x65\x73\x74\x00\x0C\x00\x00"
394+
"\x00\x68\x65\x6C\x6C\x6F\x20\x77\x6F\x72\x6C\x64\x00"
395+
"\x00")
396+
_test_conversion({u"mike": 100},
397+
"\x0F\x00\x00\x00\x10\x6D\x69\x6B\x65\x00\x64\x00\x00"
398+
"\x00\x00")
399+
_test_conversion({"hello": 1.5},
400+
"\x14\x00\x00\x00\x01\x68\x65\x6C\x6C\x6F\x00\x00\x00"
401+
"\x00\x00\x00\x00\xF8\x3F\x00")
402+
_test_conversion({"true": True},
403+
"\x0C\x00\x00\x00\x08\x74\x72\x75\x65\x00\x01\x00")
404+
_test_conversion({"false": False},
405+
"\x0D\x00\x00\x00\x08\x66\x61\x6C\x73\x65\x00\x00"
406+
"\x00")
407+
_test_conversion({"empty": []},
408+
"\x11\x00\x00\x00\x04\x65\x6D\x70\x74\x79\x00\x05\x00"
409+
"\x00\x00\x00\x00")
410+
_test_conversion({"none": {}},
411+
"\x10\x00\x00\x00\x03\x6E\x6F\x6E\x65\x00\x05\x00\x00"
412+
"\x00\x00\x00")
413+
_test_conversion({"test": Binary("test", 0)},
414+
"\x14\x00\x00\x00\x05\x74\x65\x73\x74\x00\x04\x00\x00"
415+
"\x00\x00\x74\x65\x73\x74\x00")
416+
_test_conversion({"test": Binary("test")},
417+
"\x18\x00\x00\x00\x05\x74\x65\x73\x74\x00\x08\x00\x00"
418+
"\x00\x02\x04\x00\x00\x00\x74\x65\x73\x74\x00")
419+
_test_conversion({"test": Binary("test", 128)},
420+
"\x14\x00\x00\x00\x05\x74\x65\x73\x74\x00\x04\x00\x00"
421+
"\x00\x80\x74\x65\x73\x74\x00")
422+
_test_conversion({"test": None},
423+
"\x0B\x00\x00\x00\x0A\x74\x65\x73\x74\x00\x00")
424+
_test_conversion({"date": datetime.datetime(2007, 1, 8, 0, 30, 11)},
425+
"\x13\x00\x00\x00\x09\x64\x61\x74\x65\x00\x38\xBE\x1C"
426+
"\xFF\x0F\x01\x00\x00\x00")
427+
_test_conversion({"$where": Code("test")},
428+
"\x1F\x00\x00\x00\x0F\x24\x77\x68\x65\x72\x65\x00\x12"
429+
"\x00\x00\x00\x05\x00\x00\x00\x74\x65\x73\x74\x00\x05"
430+
"\x00\x00\x00\x00\x00")
431+
a = ObjectId("\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B")
432+
_test_conversion({"oid": a},
433+
"\x16\x00\x00\x00\x07\x6F\x69\x64\x00\x00\x01\x02\x03"
434+
"\x04\x05\x06\x07\x08\x09\x0A\x0B\x00")
435+
_test_conversion({"ref": DBRef("coll", a)},
436+
"\x2F\x00\x00\x00\x03ref\x00\x25\x00\x00\x00\x02$ref"
437+
"\x00\x05\x00\x00\x00coll\x00\x07$id\x00\x00\x01\x02"
438+
"\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x00\x00")
439+
383440
if __name__ == "__main__":
384441
unittest.main()

0 commit comments

Comments
 (0)