Python library for encoding/decoding any Python object to/from JSON
—
Low-level serialization classes providing fine-grained control over the encoding/decoding process, including state management and reusability for advanced use cases.
Advanced serialization class for converting Python objects to JSON-serializable dictionaries with extensive configuration options.
class Pickler:
def __init__(
self,
unpicklable=True,
make_refs=True,
max_depth=None,
backend=None,
keys=False,
warn=False,
max_iter=None,
numeric_keys=False,
use_decimal=False,
use_base85=False,
fail_safe=None,
include_properties=False,
handle_readonly=False,
):
"""
Initialize a Pickler with configuration options.
Parameters: Same as encode() function
"""
def reset(self):
"""
Reset the pickler's internal state for reuse.
Call this between pickling sessions to clear object references.
"""
def flatten(self, obj):
"""
Convert a Python object to a JSON-serializable dictionary.
Parameters:
- obj: Python object to flatten
Returns:
JSON-serializable dictionary representation
"""Usage Example:
import jsonpickle
# Create a reusable pickler
pickler = jsonpickle.Pickler(unpicklable=False, make_refs=False)
# Flatten multiple objects
obj1 = {'name': 'Alice', 'age': 30}
obj2 = {'name': 'Bob', 'age': 25}
flattened1 = pickler.flatten(obj1)
flattened2 = pickler.flatten(obj2)
# Reset between sessions if needed
pickler.reset()
# Configure pickler for different needs
secure_pickler = jsonpickle.Pickler(
unpicklable=False,
max_depth=3,
warn=True
)Advanced deserialization class for converting JSON data back to Python objects with safety controls and state management.
class Unpickler:
def __init__(
self,
backend=None,
keys=False,
safe=True,
v1_decode=False,
on_missing='ignore',
handle_readonly=False,
):
"""
Initialize an Unpickler with configuration options.
Parameters: Same as decode() function
"""
def reset(self):
"""
Reset the unpickler's internal state for reuse.
Call this between unpickling sessions to clear object references.
"""
def restore(self, obj):
"""
Convert a JSON-serializable dictionary back to a Python object.
Parameters:
- obj: JSON-serializable dictionary to restore
Returns:
Restored Python object
"""
def register_classes(self, classes):
"""
Register classes for object construction during unpickling.
Parameters:
- classes: Single class, sequence of classes, or dict mapping names to classes
"""Usage Example:
import jsonpickle
# Create a reusable unpickler
unpickler = jsonpickle.Unpickler(safe=True, on_missing='warn')
# Register local classes
class LocalClass:
def __init__(self, value):
self.value = value
unpickler.register_classes([LocalClass])
# Restore multiple objects
json_data1 = {'py/object': '__main__.LocalClass', 'value': 42}
json_data2 = {'py/object': '__main__.LocalClass', 'value': 24}
obj1 = unpickler.restore(json_data1)
obj2 = unpickler.restore(json_data2)
# Reset between sessions if needed
unpickler.reset()
# Configure for different security levels
secure_unpickler = jsonpickle.Unpickler(
safe=True,
on_missing='error',
classes={'SafeClass': LocalClass}
)Both Pickler and Unpickler can be used as context objects in the main encode/decode functions for performance optimization.
import jsonpickle
# Reuse pickler context for multiple operations
pickler = jsonpickle.Pickler(unpicklable=False)
unpickler = jsonpickle.Unpickler(safe=True)
# Use contexts in encode/decode
obj1 = {'data': [1, 2, 3]}
obj2 = {'data': [4, 5, 6]}
json1 = jsonpickle.encode(obj1, context=pickler, reset=False)
json2 = jsonpickle.encode(obj2, context=pickler, reset=False)
restored1 = jsonpickle.decode(json1, context=unpickler, reset=False)
restored2 = jsonpickle.decode(json2, context=unpickler, reset=False)
# Reset when done
pickler.reset()
unpickler.reset()Both classes maintain internal state for object references and type handling:
Use reset() to clear this state between independent serialization sessions.
reset=False parameter preserves object references across multiple callsInstall with Tessl CLI
npx tessl i tessl/pypi-jsonpickle