A library for converting between BSON and JSON.
Project description
- Info:
See github for the latest source.
- Author:
Shane Harvey <shane.harvey@mongodb.com>
About
A fast BSON to MongoDB Extended JSON converter for Python that uses libbson.
Installation
python-bsonjs can be installed with pip:
$ python -m pip install python-bsonjs
Examples
>>> import bsonjs
>>> bson_bytes = bsonjs.loads('{"hello": "world"}')
>>> bson_bytes
'\x16\x00\x00\x00\x02hello\x00\x06\x00\x00\x00world\x00\x00'
>>> bsonjs.dumps(bson_bytes)
'{ "hello" : "world" }'
Using bsonjs with pymongo to insert a RawBSONDocument.
>>> import bsonjs
>>> from pymongo import MongoClient
>>> from bson.raw_bson import RawBSONDocument
>>> client = MongoClient("localhost", 27017, document_class=RawBSONDocument)
>>> db = client.test
>>> bson_bytes = bsonjs.loads('{"_id": 1, "x": 2}')
>>> bson_bytes
'\x15\x00\x00\x00\x10_id\x00\x01\x00\x00\x00\x10x\x00\x02\x00\x00\x00\x00'
>>> result = db.test.insert_one(RawBSONDocument(bson_bytes))
>>> result.inserted_id # NOTE: inserted_id is None
>>> result.acknowledged
True
>>> raw_doc = db.test.find_one({'x': 2})
>>> raw_doc.raw == bson_bytes
True
>>> bsonjs.dumps(raw_doc.raw)
'{ "_id" : 1, "x" : 2 }'
Speed
bsonjs is roughly 10-15x faster than PyMongo’s json_util at decoding BSON to JSON and encoding JSON to BSON. See benchmark.py:
$ python benchmark.py Timing: bsonjs.dumps(b) 10000 loops, best of 3: 0.110911846161 Timing: json_util.dumps(bson.BSON(b).decode()) 10000 loops, best of 3: 1.46571397781 bsonjs is 13.22x faster than json_util Timing: bsonjs.loads(j) 10000 loops, best of 3: 0.0628039836884 Timing: bson.BSON().encode(json_util.loads(j)) 10000 loops, best of 3: 0.683200120926 bsonjs is 11.72x faster than json_util
Limitations
Top Level Arrays
Because libbson does not distinguish between top level arrays and top level documents, neither does python-bsonjs. This means that if you give dumps or dump a top level array it will give you back a dictionary. Below are two examples of this behavior
>>> import bson
>>> from bson import json_util
>>> import bsonjs
>>> bson.decode(bsonjs.loads(json_util.dumps(["a", "b", "c"])))
{'0': 'a', '1': 'b', '2': 'c'}
>>> bson.decode(bsonjs.loads(json_util.dumps([])))
{}
One potential solution to this problem is to wrap your list in a dictionary, like so
>>> list = ["a", "b", "c"]
>>> dict = {"data": list}
>>> wrapped = bson.decode(bsonjs.loads(json_util.dumps(dict)))
{'data': ['a', 'b', 'c']}
>>> wrapped["data"]
['a', 'b', 'c']
Installing From Source
python-bsonjs supports CPython 3.6+.
Compiler
You must build python-bsonjs separately for each version of Python. On Windows this means you must use the same C compiler your Python version was built with.
Python 3.6 and up requires Microsoft Visual Studio 2015
Source
You can download the source using git:
$ git clone https://github.com/mongodb-labs/python-bsonjs.git
Install
Once you have the source properly downloaded, build and install the package:
$ python setup.py install
Test
To run the test suite:
$ python setup.py test
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for python_bsonjs-0.3.0-cp36-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13ed707edfa01c5f9aeabbb2e6e1cbb224d65201703cb966f7ef8c0e1eebb160 |
|
MD5 | 749fc135848706e8d73ff49fbc696107 |
|
BLAKE2b-256 | a11ed94b12c91b2e7f60d70aef311ab9b07a2c72492d2abca6e5be9bf7449d3c |
Hashes for python_bsonjs-0.3.0-cp36-abi3-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 47c2c82764a596f12c5d2ddfc5361df3839d62c0b25749e32a3b58e94cca8186 |
|
MD5 | fc07604b140fc63d4b668c324c2f670f |
|
BLAKE2b-256 | 891a16285ceebee06bce06c7c130de1bc599aa2bd975c4f54cac3035e10e046c |
Hashes for python_bsonjs-0.3.0-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 526c280a814dc5a0689398d3ac37ba522981712ae28cb7c4c9fb37ad442b08ec |
|
MD5 | efbfc4c1930de6ecfff6af405b548abf |
|
BLAKE2b-256 | 4ab0064bb3016b6ff16de0bbb061124f50dc265ef9d4ce166d760d1971a102cc |
Hashes for python_bsonjs-0.3.0-cp36-abi3-manylinux_2_12_i686.manylinux2010_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 16485ceab309263f56934e6dc8e98258dfbba7da81a567db28537f949d3b4f01 |
|
MD5 | 0ef8b3ab9c461f7d24c9e0be0e7fd60f |
|
BLAKE2b-256 | c2da8bdb73439721d60aba0f75b7f0a222c89f4b4cb4494ef13d7d9ff937cac5 |
Hashes for python_bsonjs-0.3.0-cp36-abi3-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e325ab88dd95e22a8011dc7c308c97a25d125d54b59f3b3922acb6cddd17c24 |
|
MD5 | 2dc3c50a5094d53091fadca4cca4af7c |
|
BLAKE2b-256 | c23f787df4714a81d9c389e982bfc3b67179f20d77ff3bbd050add53e3758d70 |
Hashes for python_bsonjs-0.3.0-cp36-abi3-macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f01180eae0c8780b6f1f248a4169847feec1d8d77626297bf8db4258bf781aca |
|
MD5 | 0cd0a9ecf8441038a8b50f5c53e31aa3 |
|
BLAKE2b-256 | 22da57360fdd9d06714167ce085393510887c21e9133f4c57df4597c14a01da3 |