json_util – Tools for using Python’s json module with BSON documents

New in version 1.1.1.

Tools for using Python’s json module with BSON documents.

This module provides two methods: object_hook and default. These names are pretty terrible, but match the names used in Python’s json library. They allow for specialized encoding and decoding of BSON documents into Mongo Extended JSON‘s Strict mode. This lets you encode / decode BSON documents to JSON even when they use special BSON types.

Example usage (serialization):

>>> json.dumps(..., default=json_util.default)

Example usage (deserialization):

>>> json.loads(..., object_hook=json_util.object_hook)

Currently this does not handle special encoding and decoding for Binary and Code instances.

Changed in version 1.9: Handle uuid.UUID instances, whenever possible.

Changed in version 1.8: Handle timezone aware datetime instances on encode, decode to timezone aware datetime instances.

Changed in version 1.8: Added support for encoding/decoding MaxKey and MinKey, and for encoding Timestamp.

Changed in version 1.2: Added support for encoding/decoding datetimes and regular expressions.


Previous topic

errors – Exceptions raised by the bson package

Next topic

max_key – Representation for the MongoDB internal MaxKey type

This Page