Amazon Ion Python Implementation
A Python implementation of Amazon Ion, a richly-typed, self-describing, hierarchical data serialization format. It offers interchangeable binary and text representations, with text being a superset of JSON. The library is currently at version 0.13.0 and is actively maintained with regular updates.
Warnings
- breaking Python 2 support was dropped in version 0.10.0. All subsequent versions require Python 3.
- breaking Support for Python versions older than 3.7 was dropped around the v0.11.1 release. Current versions are designed for Python 3.8+.
- gotcha The `simpleEnum` class was briefly removed and then re-added for backward compatibility in version 0.11.1. Depending on the exact version, code relying on `simpleEnum` might have failed.
- gotcha The library includes an optional C extension for performance with the `simpleion` module. If `cmake` is not installed or the C extension build fails, the library will silently fall back to a pure Python implementation, which may result in unexpected performance degradation.
- gotcha When using `simpleion.loads()` for Ion data that contains multiple top-level values, you must explicitly set `single_value=False` to parse the entire stream. Otherwise, it may raise an `IonException` or only load the first value.
Install
-
pip install amazon-ion
Imports
- simpleion
import amazon.ion.simpleion as ion
- IonPyTimestamp
from amazon.ion.simple_types import IonPyTimestamp
Quickstart
import amazon.ion.simpleion as ion
# Dump Python objects to Ion text or binary
data_py = {'id': 123, 'name': 'example', 'active': True}
ion_text = ion.dumps(data_py, binary=False, indent=' ')
print('Ion Text:\n', ion_text)
ion_binary = ion.dumps(data_py, binary=True)
print('Ion Binary (bytes):', ion_binary)
# Load Ion data back into Python objects
loaded_py_text = ion.loads(ion_text)
print('Loaded from Text:', loaded_py_text)
loaded_py_binary = ion.loads(ion_binary)
print('Loaded from Binary:', loaded_py_binary)
# Example with a specific Ion type
from datetime import datetime
from amazon.ion.simple_types import IonPyTimestamp
timestamp_py = IonPyTimestamp(datetime(2023, 10, 27, 10, 30, 0))
ion_timestamp = ion.dumps({'event_time': timestamp_py}, binary=False)
print('Ion Timestamp:\n', ion_timestamp)