jsonpickle
jsonpickle is a Python library for serialization and deserialization of complex Python objects to and from JSON. It extends standard JSON encoders to handle more complex data structures than what Python's `json` module natively supports. As of version 4.1.1, the project is actively maintained with a regular release cadence.
Warnings
- breaking Security Warning: Deserializing untrusted data with `jsonpickle.decode()` can lead to Remote Code Execution (RCE). Like Python's `pickle` module, `jsonpickle` can execute arbitrary code during unpickling if malicious data is provided.
- breaking Python 3.7 is no longer supported starting with `jsonpickle` v4.0.0.
- breaking The default value of the `safe` parameter in `jsonpickle.decode()` changed from `False` to `True` in v4.0.0. Setting `safe=False` enables backwards-compatible deserialization of `repr`-serialized objects but uses `eval()` and is not secure against malicious inputs.
- deprecated The `jsonpickle.compat` module is no longer used internally and may be removed in a future version (e.g., v5.0.0).
- deprecated Certain utility functions in `jsonpickle/util.py` were deprecated in v4.1.0 and are planned for removal in v5.0.0 to facilitate static typing. Additionally, `jsonpickle.ext.yaml` will no longer be registered by default in v5.0.0.
Install
-
pip install jsonpickle
Imports
- jsonpickle
import jsonpickle
Quickstart
import jsonpickle
from dataclasses import dataclass
@dataclass
class MyObject:
name: str
value: int
# Create an object
original_obj = MyObject(name="Example", value=123)
# Encode the object to a JSON string
encoded_json = jsonpickle.encode(original_obj)
print(f"Encoded JSON: {encoded_json}")
# Decode the JSON string back to a Python object
decoded_obj = jsonpickle.decode(encoded_json)
# Verify the decoded object
assert decoded_obj == original_obj
print(f"Decoded object: {decoded_obj}")