Serpent Serialization Library

1.42 · active · verified Wed Apr 15

Serpent is a simple serialization library for Python based on `ast.literal_eval`. It serializes object trees into a safe, human-readable, UTF-8 encoded string (a valid Python literal expression), suitable for data interchange between Python, Java, and .NET. As of version 1.42, it actively maintains support for modern Python versions with a release cadence of a few months to a year.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic serialization and deserialization of a Python dictionary containing various literal types. The `indent=True` option is used for human-readable output, and the data is converted to a UTF-8 string for printing.

from serpent import dumps, loads

data = {
    'name': 'Serpent Example',
    'version': 1.0,
    'is_active': True,
    'items': [1, 2, {'id': 'a'}]
}

# Serialize the data
serialized_bytes = dumps(data, indent=True) # indent=True for pretty-printing
print(f"Serialized data:\n{serialized_bytes.decode('utf-8')}")

# Deserialize the data
deserialized_data = loads(serialized_bytes)
print(f"Deserialized data: {deserialized_data}")
print(f"Is deserialized data equal to original? {data == deserialized_data}")

view raw JSON →