All Projects → mongodb-labs → python-bsonjs

mongodb-labs / python-bsonjs

Licence: Apache-2.0 license
A fast BSON to MongoDB Extended JSON converter for Python - This Repository is NOT a supported MongoDB product

Programming Languages

c
50402 projects - #5 most used programming language
python
139335 projects - #7 most used programming language
C++
36643 projects - #6 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to python-bsonjs

Json
JSON for Modern C++
Stars: ✭ 27,824 (+79397.14%)
Mutual labels:  bson
Noproto
Flexible, Fast & Compact Serialization with RPC
Stars: ✭ 138 (+294.29%)
Mutual labels:  bson
leveldb-cli
CLI for LevelDB
Stars: ✭ 86 (+145.71%)
Mutual labels:  bson
Mongo Cxx Driver
C++ Driver for MongoDB
Stars: ✭ 792 (+2162.86%)
Mutual labels:  bson
Bson
Native Swift library for BSON (http://bsonspec.org)
Stars: ✭ 83 (+137.14%)
Mutual labels:  bson
Objcmongodb
Mac OS and iOS library for MongoDB and BSON
Stars: ✭ 166 (+374.29%)
Mutual labels:  bson
Lungo
A MongoDB compatible embeddable database and toolkit for Go.
Stars: ✭ 343 (+880%)
Mutual labels:  bson
BSON
Native Swift library for BSON (http://bsonspec.org)
Stars: ✭ 98 (+180%)
Mutual labels:  bson
Kafka Connect Mongodb
**Unofficial / Community** Kafka Connect MongoDB Sink Connector - Find the official MongoDB Kafka Connector here: https://www.mongodb.com/kafka-connector
Stars: ✭ 137 (+291.43%)
Mutual labels:  bson
X2struct
Convert between json string and c++ object. json字符串和c++结构体之间互相转换
Stars: ✭ 251 (+617.14%)
Mutual labels:  bson
Jsonj
A fluent Java API for manipulating json data structures
Stars: ✭ 42 (+20%)
Mutual labels:  bson
Lua Mongo
MongoDB Driver for Lua
Stars: ✭ 81 (+131.43%)
Mutual labels:  bson
Mgodatagen
Generate random data for MongoDB
Stars: ✭ 206 (+488.57%)
Mutual labels:  bson
Alcinoe
Alcinoe Component Library For Delphi. Full opengl video player, WebRTC delphi wrapper, native ios/android TEdit, Improuved firemonkey controls, Firebase cloud messaging, Android/ios facebook sdk login, Json/Bson Parser, ImageMagick wrapper, MongoDb client And much more
Stars: ✭ 657 (+1777.14%)
Mutual labels:  bson
undatum
undatum: a command-line tool for data processing. Brings CSV simplicity to JSON lines and BSON
Stars: ✭ 39 (+11.43%)
Mutual labels:  bson
Jsoncons
A C++, header-only library for constructing JSON and JSON-like data formats, with JSON Pointer, JSON Patch, JSON Schema, JSONPath, JMESPath, CSV, MessagePack, CBOR, BSON, UBJSON
Stars: ✭ 400 (+1042.86%)
Mutual labels:  bson
Mongo Kafka
MongoDB Kafka Connector
Stars: ✭ 166 (+374.29%)
Mutual labels:  bson
flatbson
Recursively flatten a Go struct using its BSON tags
Stars: ✭ 54 (+54.29%)
Mutual labels:  bson
magnet
A JSON/BSON schema generator
Stars: ✭ 16 (-54.29%)
Mutual labels:  bson
Bson4jackson
A pluggable BSON generator and parser for the Jackson JSON processor.
Stars: ✭ 244 (+597.14%)
Mutual labels:  bson

python-bsonjs

View build status
Info:See github for the latest source.
Author:Shane Harvey <[email protected]>

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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].