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 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.9+.

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

python_bsonjs-0.6.0.tar.gz (185.6 kB view details)

Uploaded Source

Built Distributions

python_bsonjs-0.6.0-cp37-abi3-win_amd64.whl (117.1 kB view details)

Uploaded CPython 3.7+ Windows x86-64

python_bsonjs-0.6.0-cp37-abi3-win32.whl (103.3 kB view details)

Uploaded CPython 3.7+ Windows x86

python_bsonjs-0.6.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (343.7 kB view details)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ x86-64

python_bsonjs-0.6.0-cp37-abi3-manylinux_2_17_i686.manylinux2014_i686.whl (317.3 kB view details)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ i686

python_bsonjs-0.6.0-cp37-abi3-macosx_10_9_universal2.whl (211.4 kB view details)

Uploaded CPython 3.7+ macOS 10.9+ universal2 (ARM64, x86-64)

File details

Details for the file python_bsonjs-0.6.0.tar.gz.

File metadata

  • Download URL: python_bsonjs-0.6.0.tar.gz
  • Upload date:
  • Size: 185.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for python_bsonjs-0.6.0.tar.gz
Algorithm Hash digest
SHA256 8c95f6086746b7981f84c50479d1341cb52dc05aa0384118ea647863757b4374
MD5 c06ea573767e79da6b8cedc826da36f1
BLAKE2b-256 9330657e26ea020473e5fb9a37f76cd92eaff25e666f56a420650515146e8484

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_bsonjs-0.6.0.tar.gz:

Publisher: release-python.yml on mongodb-labs/python-bsonjs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python_bsonjs-0.6.0-cp37-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for python_bsonjs-0.6.0-cp37-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 d3324a265e10f1188aad19994912d724fe8225947d9f269d0b8421e03de191dc
MD5 e3ec9fb80aa83e2d54835d6b2571430b
BLAKE2b-256 729a05e500168f3a7c699b9def0260fbf645b1727f67dc98c1a40b947d62f77f

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_bsonjs-0.6.0-cp37-abi3-win_amd64.whl:

Publisher: release-python.yml on mongodb-labs/python-bsonjs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python_bsonjs-0.6.0-cp37-abi3-win32.whl.

File metadata

  • Download URL: python_bsonjs-0.6.0-cp37-abi3-win32.whl
  • Upload date:
  • Size: 103.3 kB
  • Tags: CPython 3.7+, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for python_bsonjs-0.6.0-cp37-abi3-win32.whl
Algorithm Hash digest
SHA256 a5153e272f00797129ea133c32d58ed3b28d932d06ac3f780c9fb1935f25179f
MD5 12023f962015166de3dc28ac8f506d5f
BLAKE2b-256 3bf67b62515b1ddf405fe084c55ad1b9013646952088d46391a54bfb4a9eedfb

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_bsonjs-0.6.0-cp37-abi3-win32.whl:

Publisher: release-python.yml on mongodb-labs/python-bsonjs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python_bsonjs-0.6.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for python_bsonjs-0.6.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 87c11df8a90b17851e47ddaf0c23945068761f52f5843c60a285eee50f901e69
MD5 7d572440ec4ce85b6d24e8f7f61acee4
BLAKE2b-256 de10751e4dca75174f4ee55a7ced1bf4d061227e37421aae80716119b77f85f2

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_bsonjs-0.6.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release-python.yml on mongodb-labs/python-bsonjs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python_bsonjs-0.6.0-cp37-abi3-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for python_bsonjs-0.6.0-cp37-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 43148678b2b28d1b6992bda2fbbbc71327d7c420fbab78501c1fd6b6ffc005ed
MD5 bb3244a7347a75f7005c6c6cd62f5c9f
BLAKE2b-256 e6b45925761f7e31e29735d1f329179761f1f79fe682235dd5fd260f69df9ae7

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_bsonjs-0.6.0-cp37-abi3-manylinux_2_17_i686.manylinux2014_i686.whl:

Publisher: release-python.yml on mongodb-labs/python-bsonjs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python_bsonjs-0.6.0-cp37-abi3-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for python_bsonjs-0.6.0-cp37-abi3-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 1e771e4dd9e54077d752ed9b7a193c5c539220789306bda1c6d307cb1f366977
MD5 8bf0046155f015df1ac2530b2f134e26
BLAKE2b-256 8c849c8a65afbe76611bae6ba1d1ffbd1b8a83f800b3654f1f6e937ec0761b6f

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_bsonjs-0.6.0-cp37-abi3-macosx_10_9_universal2.whl:

Publisher: release-python.yml on mongodb-labs/python-bsonjs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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