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 3-4x 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.04682216700166464
Timing: json_util.dumps(bson.decode(b))
10000 loops, best of 3: 0.17319270805455744
bsonjs is 3.70x faster than json_util

Timing: bsonjs.loads(j)
10000 loops, best of 3: 0.053156834095716476
Timing: bson.encode(json_util.loads(j))
10000 loops, best of 3: 0.15982166700996459
bsonjs is 3.01x 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.7.0.tar.gz (185.9 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

python_bsonjs-0.7.0-cp39-abi3-win_amd64.whl (117.4 kB view details)

Uploaded CPython 3.9+Windows x86-64

python_bsonjs-0.7.0-cp39-abi3-win32.whl (103.8 kB view details)

Uploaded CPython 3.9+Windows x86

python_bsonjs-0.7.0-cp39-abi3-musllinux_1_2_x86_64.whl (342.0 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ x86-64

python_bsonjs-0.7.0-cp39-abi3-musllinux_1_2_aarch64.whl (353.0 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

python_bsonjs-0.7.0-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (349.1 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

python_bsonjs-0.7.0-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl (360.1 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64manylinux: glibc 2.28+ ARM64

python_bsonjs-0.7.0-cp39-abi3-macosx_11_0_arm64.whl (108.1 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: python_bsonjs-0.7.0.tar.gz
  • Upload date:
  • Size: 185.9 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.7.0.tar.gz
Algorithm Hash digest
SHA256 ff8f3f0b48177b07b7dc082314352745db04b4a77c2f27e2b368838ed75c9099
MD5 3e3985c933037cdf56cb38c9101bf8f4
BLAKE2b-256 43acb8bd7830ce0fb25900b70231e53b418429d7d1928899fb22594426d88035

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_bsonjs-0.7.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.7.0-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: python_bsonjs-0.7.0-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 117.4 kB
  • Tags: CPython 3.9+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for python_bsonjs-0.7.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 c6dc02c2f3306fd296722abbf6f3097fc746ae4372a9177b720738a4c3df2833
MD5 d4f2a5b38ed81f40d1997aebb6133dc6
BLAKE2b-256 b3cffc5e59da6ed5a9c9bd3710040f89cf868f66348d3f89dbc1e60cecf4198f

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_bsonjs-0.7.0-cp39-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.7.0-cp39-abi3-win32.whl.

File metadata

  • Download URL: python_bsonjs-0.7.0-cp39-abi3-win32.whl
  • Upload date:
  • Size: 103.8 kB
  • Tags: CPython 3.9+, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for python_bsonjs-0.7.0-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 8999289d7aa4c3101e5557f177aeb3ed44ddefca3aa91d5351c8736f4b29eccd
MD5 a7f3a7ca71e3409a5c0a2ac31780f899
BLAKE2b-256 e1f6ef6ae3f3d16eb096a2aa55570e76127f4bc83c4c43ef37673647dc749517

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_bsonjs-0.7.0-cp39-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.7.0-cp39-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for python_bsonjs-0.7.0-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 d199ba86d947763b7df876a6bb51938774a857f720581f254e971c675f6fb2b8
MD5 c61105311e105f6524012402f826d8b0
BLAKE2b-256 f49ea22a51a0ba90d2f05de3646124bd440eb9f9498f93c62f5a0030a8c4ea17

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_bsonjs-0.7.0-cp39-abi3-musllinux_1_2_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.7.0-cp39-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for python_bsonjs-0.7.0-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 5bddca761133c0d9e331177e8586f8e20b10075774a575387b8d338d14f0a956
MD5 0f892eb6003cc6ebb5d5a77420e75830
BLAKE2b-256 19fbc743c34014fc299bd402fed71945cbf2ee68aeb53148320d6a56d40d2dc1

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_bsonjs-0.7.0-cp39-abi3-musllinux_1_2_aarch64.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.7.0-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for python_bsonjs-0.7.0-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5057948d12a5d11ffff26048e1d24b8bee4a396e1acb5a6c57ed534e3a738aec
MD5 ebcdd37c6293a82fe10a58809b1622ce
BLAKE2b-256 fa6aaac1b495d522c939a6ef96f6ff4db99c2a8486b8760482760d7bbad93062

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_bsonjs-0.7.0-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_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.7.0-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for python_bsonjs-0.7.0-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 b2bd723f6d06fdf73e8d527387f431928bdcbb38cf950c96909141d4270818c6
MD5 71d2aa8b664b9c0d38e503a469da7362
BLAKE2b-256 b018a5096f2ea56072e97e145577c242dd60f27c250ba149e4733b6291fbcfc8

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_bsonjs-0.7.0-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.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.7.0-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for python_bsonjs-0.7.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c74da3cce55ab78a02a4c75b4a8203c881d73f1b8fb47538fb562bba18a11f5d
MD5 5b73526e20dcecd8e22d0ee41494c58c
BLAKE2b-256 4e542b291b8b9d7ff4718b41404e048716999dce2bbbe6df8f301edf336777cd

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_bsonjs-0.7.0-cp39-abi3-macosx_11_0_arm64.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 Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page