Independent BSON codec for Python that doesn't depend on MongoDB
npx @tessl/cli install tessl/pypi-bson@0.5.0An independent BSON (Binary JSON) codec for Python that operates without dependency on MongoDB. It provides comprehensive serialization and deserialization capabilities for BSON format data, supporting standard Python data types and offering network socket extensions for atomic BSON object transmission.
pip install bsonpython-dateutil>=2.4.0, six>=1.9.0import bsonFor serialization and deserialization:
from bson import dumps, loadsFor ObjectId support:
from bson import ObjectIdFor network socket extensions:
from bson import patch_socketimport bson
# Serialize Python dict to BSON
data = {"name": "Alice", "age": 30, "scores": [85, 92, 78]}
bson_bytes = bson.dumps(data)
# Deserialize BSON to Python dict
restored_data = bson.loads(bson_bytes)
print(restored_data) # {'name': 'Alice', 'age': 30, 'scores': [85, 92, 78]}
# Working with ObjectIds
from bson import ObjectId
oid = ObjectId()
print(str(oid)) # e.g., '507f1f77bcf86cd799439011'
# Network socket usage
import socket
from bson import patch_socket
# Patch socket class to add BSON methods
bson.patch_socket()
# Now sockets can send/receive BSON objects
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# sock.sendobj({"message": "hello"})
# obj = sock.recvobj()The BSON package is structured around several key components:
dumps() and loads() functions for easy serializationPrimary BSON encoding and decoding functionality with support for all standard Python data types including dictionaries, lists, strings, numbers, dates, and binary data.
def dumps(obj, generator=None, on_unknown=None):
"""Convert Python object to BSON bytes"""
def loads(data):
"""Convert BSON bytes to Python object"""MongoDB-compatible ObjectId implementation providing unique identifier generation, parsing, validation, and datetime-based creation for range queries.
class ObjectId:
def __init__(self, oid=None): ...
@classmethod
def from_datetime(cls, generation_time): ...
@classmethod
def is_valid(cls, oid): ...
@property
def binary(self): ...
@property
def generation_time(self): ...Socket patching capabilities that add atomic BSON object transmission methods to Python socket objects, enabling seamless BSON communication over network connections.
def patch_socket():
"""Patch socket class with BSON methods"""
# Methods added to socket objects:
def recvbytes(self, bytes_needed, sock_buf=None): ...
def recvobj(self): ...
def sendobj(self, obj): ...Explicit integer type wrappers for precise control over BSON integer encoding, supporting 32-bit signed, 64-bit signed, and 64-bit unsigned integers.
class Int32:
def __init__(self, value): ...
def get_value(self): ...
class Int64:
def __init__(self, value): ...
def get_value(self): ...
class UInt64:
def __init__(self, value): ...
def get_value(self): ...Framework for creating custom BSON-serializable objects through the BSONCoding abstract base class, with class registration and automatic serialization/deserialization.
class BSONCoding:
def bson_encode(self): ...
def bson_init(self, raw_values): ...
def import_class(cls): ...
def import_classes(*args): ...The package supports encoding and decoding of the following BSON types:
The following BSON types are intentionally not supported for data exchange simplicity: