用于在BSON和JSON之间转换的库。
项目描述
- 信息:
查看github以获取最新源代码。
关于
Python的快速BSON到MongoDB扩展JSON转换器,使用libbson。
安装
python-bsonjs 可以通过 pip 安装。
$ python -m pip install python-bsonjs
示例
>>> 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" }'
使用 bsonjs 与 pymongo 插入 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 }'
速度
bsonjs 在解码 BSON 到 JSON 和编码 JSON 到 BSON 方面比 PyMongo 的 json_util 快约 10-15 倍。请参阅 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
限制
顶级数组
由于 libbson 区分不了顶级数组和顶级文档,python-bsonjs 也不区分。这意味着,如果您向 dumps 或 dump 提供顶级数组,它会返回一个字典。以下是两个这种行为的示例
>>> 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([])))
{}
解决这个问题的潜在解决方案是将您的列表包装在字典中,如下所示
>>> 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']
从源码安装
python-bsonjs 支持 CPython 3.8+。
编译器
您必须为每个 Python 版本单独构建 python-bsonjs。在 Windows 上,这意味着您必须使用构建 Python 版本时使用的同一 C 编译器。
Windows 构建 requires Microsoft Visual Studio 2015
源码
您可以使用 git 下载源码
$ git clone https://github.com/mongodb-labs/python-bsonjs.git
安装
一旦正确下载了源码,构建并安装包
$ pip install -v .
测试
要运行测试套件
$ python -m pytest
项目详情
下载文件
下载适合您平台的应用程序。如果您不确定选择哪个,请了解更多关于 安装包 的信息。
源代码发行版
python_bsonjs-0.5.0.tar.gz (185.5 kB 查看散列)
构建发行版
python_bsonjs-0.5.0-cp37-abi3-win_amd64.whl (120.0 kB 查看散列)
python_bsonjs-0.5.0-cp37-abi3-win32.whl (103.3 kB 查看散列)
关闭
哈希值 for python_bsonjs-0.5.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 319aea1fb63f5cb4a7b374f7856486cef1efea47a5bdb9fcdeb95a310a4f14e0 |
|
MD5 | 077a3857cb1f368cc4ed88499ff57d21 |
|
BLAKE2b-256 | b0bb0d17822513e4f5aa1f9e52b779958870d5cf0d1e81f5a6c2a4c0f80cfb05 |
关闭
哈希值 for python_bsonjs-0.5.0-cp37-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | fdcc6e85278d8dd251ca948e973fdcdae243d34cc07808b14d88bb5e6cc543e8 |
|
MD5 | 4a7e6c47f7929e285f504bb150ea1017 |
|
BLAKE2b-256 | 872b1b676615b1ea87536875794c13a6c443fec9b935cde51312c38c66d17466 |
关闭
哈希值 for python_bsonjs-0.5.0-cp37-abi3-macosx_10_9_universal2.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 547376e278779b0918ff91183e05c3c318bfed1472e1530cddf4b4750dbcbba3 |
|
MD5 | e55b938b9b3f85c59b78adf59193bd0b |
|
BLAKE2b-256 | 5619b6a77d7e4d706606ab2320d30468603949cf04fbb413c53d5ba8e536e36d |