Serializers
A serializer encodes graph payloads as bytes; the selected KV backend persists them. Choose them independently. For example, LMDB can store JSON or MessagePack, and LevelDB can store JSON or Protobuf. This flexibility applies to new compatible stores, not to reading one encoding with another decoder.
Choose a codec
Class / |
Extra |
Fit and limitations |
|---|---|---|
|
None |
Readable UTF-8, JSON-compatible properties plus GestaltDB temporal values.
Raw |
|
None |
Python-only payloads. Never unpickle untrusted data; decoding can execute code. Python object portability is not a cross-language contract. |
|
|
Compact dictionaries/lists and binary values; not arbitrary Python objects. |
|
|
Uses |
The install extra msgpack and registry name messagepack are deliberately
different. Install backend and codec extras additively, for example
python -m pip install "gestaltdb[lmdb,msgpack]". The bundle
gestaltdb[serializers] installs both optional codecs, but no backend.
JSON and temporal properties
Standalone runnable example. No optional dependency is needed. Temporal
scalars round-trip recursively through all four serializers; use the library’s
temporal values rather than assuming every Python datetime object has the
same portable encoding.
Standalone example. Choose the capability-qualified installation tab below.
Recursive GestaltDB temporal scalars round-trip through these codecs without any storage backend.
1python -m pip install "gestaltdb"
1from gestaltdb.serializers import JSONSerializer
2from gestaltdb.temporal import TemporalInstant
3
4codec = JSONSerializer()
5payload = {
6 "name": "Aspirin",
7 "observations": [{"recorded_at": TemporalInstant(0), "score": 0.9}],
8}
9encoded = codec.serialize(payload)
10assert isinstance(encoded, bytes)
11assert codec.deserialize(encoded) == payload
1python -m pip install "gestaltdb"
1from gestaltdb.serializers import PickleSerializer
2from gestaltdb.temporal import TemporalInstant
3
4codec = PickleSerializer()
5payload = {
6 "name": "Aspirin",
7 "observations": [{"recorded_at": TemporalInstant(0), "score": 0.9}],
8}
9encoded = codec.serialize(payload)
10assert isinstance(encoded, bytes)
11assert codec.deserialize(encoded) == payload
1python -m pip install "gestaltdb[msgpack]"
1from gestaltdb.serializers import MessagePackSerializer
2from gestaltdb.temporal import TemporalInstant
3
4codec = MessagePackSerializer()
5payload = {
6 "name": "Aspirin",
7 "observations": [{"recorded_at": TemporalInstant(0), "score": 0.9}],
8}
9encoded = codec.serialize(payload)
10assert isinstance(encoded, bytes)
11assert codec.deserialize(encoded) == payload
1python -m pip install "gestaltdb[protobuf]"
1from gestaltdb.serializers import ProtobufSerializer
2from gestaltdb.temporal import TemporalInstant
3
4codec = ProtobufSerializer()
5payload = {
6 "name": "Aspirin",
7 "observations": [{"recorded_at": TemporalInstant(0), "score": 0.9}],
8}
9encoded = codec.serialize(payload)
10assert isinstance(encoded, bytes)
11assert codec.deserialize(encoded) == payload
The encoded temporal representation uses library tags; it is not just an untyped date string. Cypher temporal constructors and exact/range property indexes have their own semantics (Cypher Queries, Property-Graph Filtering and Indexes). Scalar property values do not automatically create temporal history.
Binary values with MessagePack
Standalone runnable example. Requires gestaltdb[msgpack].
Standalone example. Choose the capability-qualified installation tab below.
These codecs support the raw byte payload. JSON does not. Pickle is for trusted data only.
1python -m pip install "gestaltdb[msgpack]"
1from gestaltdb.serializers import MessagePackSerializer
2
3codec = MessagePackSerializer()
4payload = {"edge_ids": [b"e1", b"e2"]}
5assert codec.deserialize(codec.serialize(payload)) == payload
1python -m pip install "gestaltdb"
1from gestaltdb.serializers import PickleSerializer
2
3codec = PickleSerializer()
4payload = {"edge_ids": [b"e1", b"e2"]}
5assert codec.deserialize(codec.serialize(payload)) == payload
1python -m pip install "gestaltdb[protobuf]"
1from gestaltdb.serializers import ProtobufSerializer
2
3codec = ProtobufSerializer()
4payload = {"edge_ids": [b"e1", b"e2"]}
5assert codec.deserialize(codec.serialize(payload)) == payload
Protobuf integer and bytes preservation
Standalone runnable example. Requires gestaltdb[protobuf]. Integer/bytes
tags preserve values that ordinary Struct numeric/string fields would not
represent in the same way.
Standalone example. Choose the capability-qualified installation tab below.
These choices preserve the large integer and bytes. Plain JSON cannot encode this raw-byte payload.
1python -m pip install "gestaltdb[protobuf]"
1from gestaltdb.serializers import ProtobufSerializer
2
3codec = ProtobufSerializer()
4payload = {"count": 2**60 + 1, "raw": b"abc"}
5assert codec.deserialize(codec.serialize(payload)) == payload
1python -m pip install "gestaltdb[msgpack]"
1from gestaltdb.serializers import MessagePackSerializer
2
3codec = MessagePackSerializer()
4payload = {"count": 2**60 + 1, "raw": b"abc"}
5assert codec.deserialize(codec.serialize(payload)) == payload
1python -m pip install "gestaltdb"
1from gestaltdb.serializers import PickleSerializer
2
3codec = PickleSerializer()
4payload = {"count": 2**60 + 1, "raw": b"abc"}
5assert codec.deserialize(codec.serialize(payload)) == payload
Pickle for trusted Python-only data
Standalone runnable example. No optional dependency is needed. This payload is created and decoded locally; it is not obtained from an untrusted supplier.
Standalone example. Choose the capability-qualified installation tab below.
This Python set payload is intentionally Pickle-specific; other codecs are not equivalent.
1python -m pip install "gestaltdb"
1from gestaltdb.serializers import PickleSerializer
2
3codec = PickleSerializer()
4payload = {"ids": {"drug-1", "protein-1"}}
5assert codec.deserialize(codec.serialize(payload)) == payload
Using a serializer with GraphDB
Standalone runnable example. Requires gestaltdb[lmdb,msgpack].
Standalone example. Choose the capability-qualified installation tab below.
Backend choice is independent, but this binary property excludes JSON. Reopen always uses the original manifest codec.
1python -m pip install "gestaltdb[lmdb,msgpack]"
1from tempfile import TemporaryDirectory
2from gestaltdb.graphdb import GraphDB, Node
3
4with TemporaryDirectory() as root:
5 path = f"{root}/graph"
6 with GraphDB.create(path, backend='lmdb', serializer='messagepack') as graph:
7 graph.put_node(Node("sample-1", properties={"raw": b"abc"}))
8 with GraphDB.open(path) as reopened:
9 assert reopened.get_node("sample-1").properties["raw"] == b"abc"
1python -m pip install "gestaltdb[lmdb]"
1from tempfile import TemporaryDirectory
2from gestaltdb.graphdb import GraphDB, Node
3
4with TemporaryDirectory() as root:
5 path = f"{root}/graph"
6 with GraphDB.create(path, backend='lmdb', serializer='pickle') as graph:
7 graph.put_node(Node("sample-1", properties={"raw": b"abc"}))
8 with GraphDB.open(path) as reopened:
9 assert reopened.get_node("sample-1").properties["raw"] == b"abc"
1python -m pip install "gestaltdb[lmdb,protobuf]"
1from tempfile import TemporaryDirectory
2from gestaltdb.graphdb import GraphDB, Node
3
4with TemporaryDirectory() as root:
5 path = f"{root}/graph"
6 with GraphDB.create(path, backend='lmdb', serializer='protobuf') as graph:
7 graph.put_node(Node("sample-1", properties={"raw": b"abc"}))
8 with GraphDB.open(path) as reopened:
9 assert reopened.get_node("sample-1").properties["raw"] == b"abc"
1python -m pip install "gestaltdb[leveldb,msgpack]"
1from tempfile import TemporaryDirectory
2from gestaltdb.graphdb import GraphDB, Node
3
4with TemporaryDirectory() as root:
5 path = f"{root}/graph"
6 with GraphDB.create(path, backend='leveldb', serializer='messagepack') as graph:
7 graph.put_node(Node("sample-1", properties={"raw": b"abc"}))
8 with GraphDB.open(path) as reopened:
9 assert reopened.get_node("sample-1").properties["raw"] == b"abc"
1python -m pip install "gestaltdb[leveldb]"
1from tempfile import TemporaryDirectory
2from gestaltdb.graphdb import GraphDB, Node
3
4with TemporaryDirectory() as root:
5 path = f"{root}/graph"
6 with GraphDB.create(path, backend='leveldb', serializer='pickle') as graph:
7 graph.put_node(Node("sample-1", properties={"raw": b"abc"}))
8 with GraphDB.open(path) as reopened:
9 assert reopened.get_node("sample-1").properties["raw"] == b"abc"
1python -m pip install "gestaltdb[leveldb,protobuf]"
1from tempfile import TemporaryDirectory
2from gestaltdb.graphdb import GraphDB, Node
3
4with TemporaryDirectory() as root:
5 path = f"{root}/graph"
6 with GraphDB.create(path, backend='leveldb', serializer='protobuf') as graph:
7 graph.put_node(Node("sample-1", properties={"raw": b"abc"}))
8 with GraphDB.open(path) as reopened:
9 assert reopened.get_node("sample-1").properties["raw"] == b"abc"
1python -m pip install "gestaltdb[msgpack,rocksdb]"
1from tempfile import TemporaryDirectory
2from gestaltdb.graphdb import GraphDB, Node
3
4with TemporaryDirectory() as root:
5 path = f"{root}/graph"
6 with GraphDB.create(path, backend='pyrex', serializer='messagepack') as graph:
7 graph.put_node(Node("sample-1", properties={"raw": b"abc"}))
8 with GraphDB.open(path) as reopened:
9 assert reopened.get_node("sample-1").properties["raw"] == b"abc"
1python -m pip install "gestaltdb[rocksdb]"
1from tempfile import TemporaryDirectory
2from gestaltdb.graphdb import GraphDB, Node
3
4with TemporaryDirectory() as root:
5 path = f"{root}/graph"
6 with GraphDB.create(path, backend='pyrex', serializer='pickle') as graph:
7 graph.put_node(Node("sample-1", properties={"raw": b"abc"}))
8 with GraphDB.open(path) as reopened:
9 assert reopened.get_node("sample-1").properties["raw"] == b"abc"
1python -m pip install "gestaltdb[protobuf,rocksdb]"
1from tempfile import TemporaryDirectory
2from gestaltdb.graphdb import GraphDB, Node
3
4with TemporaryDirectory() as root:
5 path = f"{root}/graph"
6 with GraphDB.create(path, backend='pyrex', serializer='protobuf') as graph:
7 graph.put_node(Node("sample-1", properties={"raw": b"abc"}))
8 with GraphDB.open(path) as reopened:
9 assert reopened.get_node("sample-1").properties["raw"] == b"abc"
For direct construction import codec classes from gestaltdb.serializers and
pass an instance to GraphDB(store, codec). Import backend classes separately
from gestaltdb.kvstores. The complete lifecycle example and the distinction
between direct construction and manifest-backed stores are in
Storage Backends.
Compatibility and trust boundaries
Reopen a store with its original compatible codec. Editing the manifest’s serializer name or rebinding a decoder is not a data migration.
Temporal values are supported across codecs, but arbitrary Python payload types are not universally interchangeable. Validate the values your application uses.
Readable or tagged encoding is not source authentication, encryption, a resource sandbox, or a guarantee that supplier assertions are true.
Codecs alone do not provide storage transactions, index maintenance or safe publication. These are graph/backend responsibilities.
See Compatibility, Migration, and Recovery for compatibility and recovery, and Performance and Benchmarks for why serialization costs should be measured separately from backend I/O.