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.7+.
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.
Windows build 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:
$ pip install -v .
Test
To run the test suite:
$ python -m pytest
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.4.0-cp37-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3e571a66848e89ee51928fc7f1b2561c85b53816b703ff6a770dbb6fa839c75c |
|
MD5 | ebb94da09befc4c53d6c2a95868c70c2 |
|
BLAKE2b-256 | ff6ef40d12c638a4b9db593982dd1ecfd249abe2489d2a867e6100bc70b3f092 |
Hashes for python_bsonjs-0.4.0-cp37-abi3-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c7e892a3c57da599b4c4dc707bba9f65c5b1c54e2a1b78694622aed9f4e41b8e |
|
MD5 | c9ee638df20042f296a68494558d041c |
|
BLAKE2b-256 | 6d4d93c13279f8b0d1d8f93f7a83a3fb7677d7bca48238017f427e3f209c104f |
Hashes for python_bsonjs-0.4.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c213fa51b6abb75b565aa6d3fa1c6d80def68d9df5c2a93e050e2a7e1a06b35f |
|
MD5 | dfd4fca00b92027cd2633c2c6435ad64 |
|
BLAKE2b-256 | 0c07a035e832377fbc33ce5e33c60125d6b697b166806546a6c084b7b7e575b5 |
Hashes for python_bsonjs-0.4.0-cp37-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88886529219c096c870f24541ce73399bed81f872bc4b1cfecbf2cbd1c6df068 |
|
MD5 | 72faeee6fec4781688e179b5ed71d3e6 |
|
BLAKE2b-256 | a1992c8b4f533fbddac96da6c17329d08ffe128dd65afab2fd952f2ec5dc22b3 |
Hashes for python_bsonjs-0.4.0-cp37-abi3-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 481bf8b8e6a0013f07834f8429e8dd48543e24fb0223ed05258092aba5eba6da |
|
MD5 | 69c441a0920a933596ad15ec96b3ead0 |
|
BLAKE2b-256 | b6c07445de9ad964d60cb62bf15db05f154f62d54eda4845db67ac6dbee0dd6f |
Hashes for python_bsonjs-0.4.0-cp37-abi3-macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2e3ebeeec3632e3b40e060126d0a211f51a2f56b0526ef1dffeac14e9f2819fb |
|
MD5 | b9013f0bdab0acc8d4867140f3d3314d |
|
BLAKE2b-256 | b1cc8d0d39cdf1005db13541b3856f38f03eeaacf8519a6ad2ba78393e83cd9a |