Serialization¶
Serialization classes provide binary and token-based encoding/decoding.
When to use: Use Value.encode() / Value.decode() for binary
serialization. Use streams for low-level I/O and custom protocols.
Quick Start¶
>>> from dsviper import Value, ValueString, Type, TypeVector, Definitions
>>> defs = Definitions().const()
>>> blob = Value.encode(ValueString("hello"))
>>> blob
blob(13)
Decoding a primitive hands back the Python object directly, not a Value:
>>> Value.decode(blob, Type.STRING, defs)
'hello'
A container comes back as a Value. The type passed to decode is the one
the blob was encoded from:
>>> t_vec = TypeVector(Type.INT64)
>>> blob = Value.encode(Value.create(t_vec, [1, 2, 3]))
>>> Value.dumps(Value.decode(blob, t_vec, defs))
[1, 2, 3]
JSON Serialization¶
For web integration and REST APIs, use Value.json_encode() and Value.json_decode().
from dsviper import Value, ValueString, Type, Definitions
# Encode primitive to JSON
value = ValueString("hello")
json_str = Value.json_encode(value) # '"hello"'
# Decode JSON (requires type and definitions)
defs = Definitions()
decoded = Value.json_decode(json_str, Type.STRING, defs.const())
Flask REST API Example¶
Real-world pattern for exposing Viper C++ data via REST:
from flask import Flask, make_response
from dsviper import CommitDatabase, CommitStateBuilder, Value
app = Flask(__name__)
@app.get("/material/<uuid:instance_id>")
def material_api(instance_id):
db = CommitDatabase.open("model.cdb")
doc = CommitStateBuilder.state(db, db.last_commit_id()).attachment_getting().get(
MY_APP_A_MATERIAL_RENDER, ValueKey.create(MY_APP_T_MATERIAL, str(instance_id))
)
db.close()
# Encode document to JSON with indentation
json_string = Value.json_encode(doc, indent=2)
res = make_response(json_string)
res.mimetype = "application/json"
return res
DSM schema can also be exported for client-side validation:
@app.get("/schema")
def schema_api():
db = CommitDatabase.open("model.cdb")
dsm_defs = db.definitions().to_dsm_definitions()
db.close()
json_string = dsm_defs.json_encode(indent=2)
res = make_response(json_string)
res.mimetype = "application/json"
return res
XML Serialization¶
The XML dialect of the type-driven serializer, alongside JSON — use
Value.to_xml_string() / Value.from_xml_string() when a downstream
consumer expects XML. As with JSON, decoding requires the type and definitions.
from dsviper import Value, ValueString, Type, Definitions
# Encode primitive to XML
value = ValueString("hello")
xml_str = Value.to_xml_string(value, indent=2)
# Decode XML (requires type and definitions)
defs = Definitions()
decoded = Value.from_xml_string(xml_str, Type.STRING, defs.const())
A parsed DSM schema serializes to XML the same way, via
DSMDefinitions.to_xml_string(indent=...) / DSMDefinitions.from_xml_string(string).
Choosing the Right Approach¶
Use Case |
API |
Note |
|---|---|---|
Binary serialization |
|
Default codec |
Binary deserialization |
|
Requires type + definitions |
JSON for web APIs |
|
REST/web integration |
XML for XML consumers |
|
Same type-driven codec, XML dialect |
Custom codec |
|
Raw, binary, token formats |
File I/O |
|
Low-level streaming |
Codec¶
The registry of named codecs: ask for one by name with query() or check(), or take STREAM_RAW, STREAM_BINARY or STREAM_TOKEN_BINARY directly. |
|
Creates the encoder, the decoder and the sizer of one codec. |
|
Generates random values against a set of definitions. |
Binary Streams¶
Reads values in a fixed byte order, portable across machines. |
|
Writes values in a fixed byte order, portable across machines. |
|
Reads a portable binary stream that carries a type token per value. |
|
Writes a portable binary stream with a type token per value. |
Stream I/O¶
Reads data from a blob. |
|
Reads data from a file. |
|
Reads data from a shared memory region. |
|
Writes data to a blob. |
|
Writes data to a file. |
|
Writes data to a shared memory region. |
Protocols¶
The typed read interface. |
|
The typed write interface. |
|
Writes typed values into a blob. |
|
Reads typed values back from a blob. |
|
Computes how many bytes the fixed-width types occupy under one codec. |
Raw Streams¶
Reads values in the machine's own byte order. |
|
Writes values in the machine's own byte order. |
|
An opaque handle on a byte source, whose one operation is size(). |
|
An opaque handle on a byte sink, with no operation of its own. |