Python library for encoding/decoding any Python object to/from JSON
npx @tessl/cli install tessl/pypi-jsonpickle@4.1.0A Python library for encoding/decoding any Python object to/from JSON format. jsonpickle builds upon existing JSON encoders (simplejson, json, ujson) and provides comprehensive support for complex Python objects including custom classes, numpy arrays, pandas dataframes, and other objects that cannot be directly serialized to JSON.
Security Warning: jsonpickle is not secure. Only unpickle data you trust. It can execute arbitrary code during unpickling, similar to Python's pickle module.
pip install jsonpickleimport jsonpickleFor advanced usage:
from jsonpickle import Pickler, Unpickler, register, unregister
from jsonpickle.backend import JSONBackendimport jsonpickle
# Define a custom class
class Thing:
def __init__(self, name):
self.name = name
obj = Thing('Awesome')
# Encode to JSON string
json_string = jsonpickle.encode(obj)
# Decode back to Python object
restored_obj = jsonpickle.decode(json_string)
assert obj.name == restored_obj.nameFor one-way serialization (no restoration capability):
# Create simpler JSON without restoration metadata
oneway = jsonpickle.encode(obj, unpicklable=False)
result = jsonpickle.decode(oneway)
assert obj.name == result['name'] == 'Awesome'jsonpickle uses a modular architecture with several key components:
This design allows jsonpickle to handle arbitrary Python objects while maintaining compatibility with multiple JSON backends and providing security controls for safe deserialization.
Essential encode/decode functions for converting Python objects to/from JSON strings, with extensive configuration options for controlling serialization behavior.
def encode(
value,
unpicklable=True,
make_refs=True,
keys=False,
max_depth=None,
reset=True,
backend=None,
warn=False,
context=None,
max_iter=None,
use_decimal=False,
numeric_keys=False,
use_base85=False,
fail_safe=None,
indent=None,
separators=None,
include_properties=False,
handle_readonly=False,
) -> strdef decode(
string,
backend=None,
context=None,
keys=False,
reset=True,
safe=True,
classes=None,
v1_decode=False,
on_missing='ignore',
handle_readonly=False,
) -> any# JSON compatibility aliases
dumps = encode
loads = decodeLow-level serialization classes providing fine-grained control over the encoding/decoding process, including state management and reusability.
class Pickler:
def __init__(self, **options): ...
def reset(self): ...
def flatten(self, obj): ...
class Unpickler:
def __init__(self, **options): ...
def reset(self): ...
def restore(self, obj): ...
def register_classes(self, classes): ...Extensible system for registering custom serialization handlers for specific types, allowing you to control how particular objects are encoded and decoded.
def register(cls, handler=None, base=False): ...
def unregister(cls): ...
class BaseHandler:
def __init__(self, context): ...
def flatten(self, obj, data): ...
def restore(self, data): ...Pluggable JSON encoder/decoder backend system supporting multiple JSON libraries with configuration options and fallback mechanisms.
class JSONBackend:
def encode(self, obj): ...
def decode(self, s): ...
def load_backend(self, name): ...
def set_preferred_backend(self, name): ...
def set_encoder_options(self, name, **options): ...
def set_decoder_options(self, name, **options): ...
# Module-level backend functions (exported to jsonpickle namespace)
def set_preferred_backend(name): ...
def set_encoder_options(name, **options): ...
def set_decoder_options(name, **options): ...
def load_backend(name): ...
def remove_backend(name): ...
def enable_fallthrough(enable): ...Built-in extensions for popular Python libraries including numpy, pandas, GMPY, and YAML integration.
# NumPy extension
from jsonpickle.ext import numpy
numpy.register_handlers()
numpy.unregister_handlers()
# Pandas extension
from jsonpickle.ext import pandas
pandas.register_handlers()
pandas.unregister_handlers()
# GMPY extension
from jsonpickle.ext import gmpy
gmpy.register_handlers()
gmpy.unregister_handlers()
# YAML backend
from jsonpickle.ext import yaml
yaml.register()# Core exception class
class ClassNotFoundError(BaseException):
"""Raised when a class cannot be found during unpickling"""
pass
# Version information
__version__: str