Skip to main content

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.

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

Installing From Source

python-bsonjs supports CPython 2.6, 2.7, and 3.3+.

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.

Source

Note that this repository contains a git submodule. You must initialize the submodule by using:

$ git clone --recursive https://github.com/mongodb-labs/python-bsonjs.git

Or, if your version of git does not have clone --recursive:

$ git clone https://github.com/mongodb-labs/python-bsonjs.git
$ cd python-bsonjs
$ git submodule update --init --recursive

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

python-bsonjs-0.1.0.tar.gz (142.0 kB view details)

Uploaded Source

Built Distributions

python_bsonjs-0.1.0-cp35-cp35m-manylinux1_x86_64.whl (256.5 kB view details)

Uploaded CPython 3.5m

python_bsonjs-0.1.0-cp35-cp35m-manylinux1_i686.whl (235.0 kB view details)

Uploaded CPython 3.5m

python_bsonjs-0.1.0-cp35-cp35m-macosx_10_6_intel.whl (156.9 kB view details)

Uploaded CPython 3.5mmacOS 10.6+ Intel (x86-64, i386)

python_bsonjs-0.1.0-cp34-cp34m-manylinux1_x86_64.whl (256.3 kB view details)

Uploaded CPython 3.4m

python_bsonjs-0.1.0-cp34-cp34m-manylinux1_i686.whl (234.8 kB view details)

Uploaded CPython 3.4m

python_bsonjs-0.1.0-cp34-cp34m-macosx_10_6_intel.whl (156.8 kB view details)

Uploaded CPython 3.4mmacOS 10.6+ Intel (x86-64, i386)

python_bsonjs-0.1.0-cp33-cp33m-manylinux1_x86_64.whl (256.2 kB view details)

Uploaded CPython 3.3m

python_bsonjs-0.1.0-cp33-cp33m-manylinux1_i686.whl (234.8 kB view details)

Uploaded CPython 3.3m

python_bsonjs-0.1.0-cp27-cp27mu-manylinux1_x86_64.whl (255.2 kB view details)

Uploaded CPython 2.7mu

python_bsonjs-0.1.0-cp27-cp27mu-manylinux1_i686.whl (234.6 kB view details)

Uploaded CPython 2.7mu

python_bsonjs-0.1.0-cp27-cp27m-manylinux1_x86_64.whl (255.2 kB view details)

Uploaded CPython 2.7m

python_bsonjs-0.1.0-cp27-cp27m-manylinux1_i686.whl (234.6 kB view details)

Uploaded CPython 2.7m

python_bsonjs-0.1.0-cp27-cp27m-macosx_10_11_intel.whl (143.1 kB view details)

Uploaded CPython 2.7mmacOS 10.11+ Intel (x86-64, i386)

python_bsonjs-0.1.0-cp26-cp26mu-manylinux1_x86_64.whl (255.2 kB view details)

Uploaded CPython 2.6mu

python_bsonjs-0.1.0-cp26-cp26mu-manylinux1_i686.whl (234.6 kB view details)

Uploaded CPython 2.6mu

python_bsonjs-0.1.0-cp26-cp26m-manylinux1_x86_64.whl (255.2 kB view details)

Uploaded CPython 2.6m

python_bsonjs-0.1.0-cp26-cp26m-manylinux1_i686.whl (234.6 kB view details)

Uploaded CPython 2.6m

python_bsonjs-0.1.0-cp26-cp26m-macosx_10_11_intel.whl (143.1 kB view details)

Uploaded CPython 2.6mmacOS 10.11+ Intel (x86-64, i386)

File details

Details for the file python-bsonjs-0.1.0.tar.gz.

File metadata

  • Download URL: python-bsonjs-0.1.0.tar.gz
  • Upload date:
  • Size: 142.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for python-bsonjs-0.1.0.tar.gz
Algorithm Hash digest
SHA256 09acbb7617c3d2e275e29a157cb40ff3e3c0561230581316d141eba7df3fc14f
MD5 a89eeaf52db968f34b72086b0dee2bc3
BLAKE2b-256 dd851897718b503b45f421450e4f0ed18bc11a179c8ffe1c67356a6ce6169c51

See more details on using hashes here.

File details

Details for the file python_bsonjs-0.1.0-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for python_bsonjs-0.1.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 2911a930bf2c1799247af26651f6e4a1be40861de857486ad2f2ff2b9451fbf7
MD5 dd9497b29786dd97e9c0ecf8dd58e850
BLAKE2b-256 e8fd9a87bab77f7db6002aa75dace17a39751c73aabe43ed9a825ce78c752b91

See more details on using hashes here.

File details

Details for the file python_bsonjs-0.1.0-cp35-cp35m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for python_bsonjs-0.1.0-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 d63ed960c947f4c629be2356de66c01385a5f4c3253e4908a4ae65080c4ac4ce
MD5 0a668b9300c3878f988ebf9bffdfc957
BLAKE2b-256 69a02a9324609862b4628e13408fc36ba3382ce71743be7c7aed96cdf28944a4

See more details on using hashes here.

File details

Details for the file python_bsonjs-0.1.0-cp35-cp35m-macosx_10_6_intel.whl.

File metadata

File hashes

Hashes for python_bsonjs-0.1.0-cp35-cp35m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 e1a20b0eb45a42ce22de23ef6462b36f8264fa2a2ddaec34f1004b5800b0e599
MD5 752023be6b8ec8a192320dcc43248d7a
BLAKE2b-256 f57645881e31017e88e7c90d65666a8c85f3c53c5e16a708236851f4a8a6633e

See more details on using hashes here.

File details

Details for the file python_bsonjs-0.1.0-cp34-cp34m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for python_bsonjs-0.1.0-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6d0968ea150522e554f8c9e83b06c4b26de6a045d36188b9052f9adc007add86
MD5 3d7f0e3011ca08595260a3ef0fa5b6bb
BLAKE2b-256 dbe6961e2720c99eec5f27c641df25322c10d1dad6b590dde9be54dddd189c4b

See more details on using hashes here.

File details

Details for the file python_bsonjs-0.1.0-cp34-cp34m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for python_bsonjs-0.1.0-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 08ef69d103e7f0499bd924f164834f1a1251515bf4d865b4380f06f441b3f1de
MD5 dbac45ad9562a10a87c56d94569209a6
BLAKE2b-256 c96fc6675b645c1defaa0ec973677dcf3dc4c464c2b25ef69cecb125af5bcb81

See more details on using hashes here.

File details

Details for the file python_bsonjs-0.1.0-cp34-cp34m-macosx_10_6_intel.whl.

File metadata

File hashes

Hashes for python_bsonjs-0.1.0-cp34-cp34m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 bb3d521a5c1cf741fe301b457ac5f149583326ec98d47984efeadad2bf578dc2
MD5 39d3a947a6ffae5854d5a04825467c1e
BLAKE2b-256 3c24810020c55191e8f6befbbfddaa49a47d410c841d787477ed54d94ae320ee

See more details on using hashes here.

File details

Details for the file python_bsonjs-0.1.0-cp33-cp33m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for python_bsonjs-0.1.0-cp33-cp33m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 fda53c6a1b2332bf15cfaf638fb4b18a26f56cd1def555bd3617c6dabc24b47b
MD5 efb67a9a7889bfe08e28e68991669335
BLAKE2b-256 96aebac24502a57dae8ff734441620ad4eb92ec68c71420a46971dc619ce5fa0

See more details on using hashes here.

File details

Details for the file python_bsonjs-0.1.0-cp33-cp33m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for python_bsonjs-0.1.0-cp33-cp33m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 2b5f8eabc9e5589d768f489b86ae87862550e3df1f1929242657f52927455ebd
MD5 2dfaff7eff75083091d1425d4623b153
BLAKE2b-256 d57f6fb7ba5a2eebe94f63d7520a53fd8320ab17e2d48b5a498f9cf34e77d643

See more details on using hashes here.

File details

Details for the file python_bsonjs-0.1.0-cp27-cp27mu-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for python_bsonjs-0.1.0-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 791422eda169d99815c029aaf2bd1239e07658d7a6651ed8a3dbb5e9f9256669
MD5 8ab808f0069413f76c0763e624c7f2ad
BLAKE2b-256 effb7dd3a20a50c0af376246fa0c915bc343a838b048b0ca937666ee72bb7b9c

See more details on using hashes here.

File details

Details for the file python_bsonjs-0.1.0-cp27-cp27mu-manylinux1_i686.whl.

File metadata

File hashes

Hashes for python_bsonjs-0.1.0-cp27-cp27mu-manylinux1_i686.whl
Algorithm Hash digest
SHA256 a4361b14599bb5dd5ac66f436ece3226e2f3c0b245506d2c462019f021fad117
MD5 f289d0a01190dd748c6e990697847f85
BLAKE2b-256 9c3765bc49ff82ee67e2bb0a3149d314831fdf9fb0250cf7dfecd476bcc74c9a

See more details on using hashes here.

File details

Details for the file python_bsonjs-0.1.0-cp27-cp27m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for python_bsonjs-0.1.0-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4c512277137002c6faf1ddae9cf9edfeb7b6a6e5638088f3e62caef6c55182c6
MD5 ec86525caa94518b9efb282f232a0565
BLAKE2b-256 91a4f6f1ca1418a6da0dbd18dd0f6bd3572dba2c984774a63d43604ffac7ac82

See more details on using hashes here.

File details

Details for the file python_bsonjs-0.1.0-cp27-cp27m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for python_bsonjs-0.1.0-cp27-cp27m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 cdf913f20df3116d4d6f8e5b3032d00cb99135ddedcca54df737a0d6c3c33642
MD5 0f24c01102d9af90e90cdb5d81c0bd21
BLAKE2b-256 93ca151884cee640b16c1fc68918c2cae37f32fbf19e05b7aa68c1b08b7270d2

See more details on using hashes here.

File details

Details for the file python_bsonjs-0.1.0-cp27-cp27m-macosx_10_11_intel.whl.

File metadata

File hashes

Hashes for python_bsonjs-0.1.0-cp27-cp27m-macosx_10_11_intel.whl
Algorithm Hash digest
SHA256 cc208edecf07f5ac02c1011add1ff309240d28c549f3d679a51d4127f985f18d
MD5 7707432b4ef68261eeed5e3a53f45390
BLAKE2b-256 a9717fbd04eb96d27a96caf1a46ea9422308cd8fdce6033c83abedac615d156b

See more details on using hashes here.

File details

Details for the file python_bsonjs-0.1.0-cp26-cp26mu-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for python_bsonjs-0.1.0-cp26-cp26mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7cdc9ab80e0350c2b3ef0e88223842b13028cf2411f2f92a7a7877526b97a292
MD5 e428595b112c0cacca491b9425359e03
BLAKE2b-256 e3122a1da73cb32a1f835ef35254fc91cc2fcca2362d3acf9c76d233f2aae2df

See more details on using hashes here.

File details

Details for the file python_bsonjs-0.1.0-cp26-cp26mu-manylinux1_i686.whl.

File metadata

File hashes

Hashes for python_bsonjs-0.1.0-cp26-cp26mu-manylinux1_i686.whl
Algorithm Hash digest
SHA256 e7daec1d6deca3224ae94f7be067984cfdfc82f6b157a8dcd258889b19003c40
MD5 8e18e46300b4e2422d506d73f36efbae
BLAKE2b-256 a4a3d083aa07631558a9bdedf581e8e01177153782e10f46e93c3973df40d2a3

See more details on using hashes here.

File details

Details for the file python_bsonjs-0.1.0-cp26-cp26m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for python_bsonjs-0.1.0-cp26-cp26m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 693f62b8adda158599be1a9cd001b75e354250ac848ae928fef6dea6b82bb0b8
MD5 08b52c6e85b741194a30132630fbfdaa
BLAKE2b-256 e8f8df42b1ae77346deae8efdc8e1a362fbd3054f9d2d63da19907e8765c62ba

See more details on using hashes here.

File details

Details for the file python_bsonjs-0.1.0-cp26-cp26m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for python_bsonjs-0.1.0-cp26-cp26m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 a6f0f704b986dd762b59f5ad289bd584d796e4160d46ec99f6dff1fc228fb6db
MD5 ae5574bd584f4bcdd6dca6eb8f57abe1
BLAKE2b-256 1d6e6059b62dd2739b54646d8c01992019760edef0843acf6e1dc98619891da4

See more details on using hashes here.

File details

Details for the file python_bsonjs-0.1.0-cp26-cp26m-macosx_10_11_intel.whl.

File metadata

File hashes

Hashes for python_bsonjs-0.1.0-cp26-cp26m-macosx_10_11_intel.whl
Algorithm Hash digest
SHA256 ca4f6f2af5806c0834dcd6882c29dfb663b9f03bbcce6952566bec0482ca770d
MD5 ae4b2cbcf633b799162a52ca99cb206c
BLAKE2b-256 9b4e00c1acecd23414d23e172e92612ae94425ef182c39d83d44a0228f9d2b93

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page