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, Definitions
# Create a value
value = ValueString("hello")
# Encode to binary blob
blob = Value.encode(value)
# Decode from binary (requires type and definitions)
defs = Definitions()
decoded = Value.decode(blob, Type.STRING, defs.const())
# Works with any type
from dsviper import TypeVector, ValueVector
t_vec = TypeVector(Type.INT64)
vec = Value.create(t_vec, [1, 2, 3])
blob = Value.encode(vec)
decoded = Value.decode(blob, t_vec, defs.const())
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 data via REST:
from flask import Flask, make_response
from dsviper import CommitDatabase, Value
app = Flask(__name__)
@app.get("/material/<uuid:instance_id>")
def material_api(instance_id):
db = CommitDatabase.open("model.cdb")
doc = db.state(db.last_commit_id()).attachment_getting().get(
MYAPP_A_Material, ValueKey.create(MYAPP_C_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
Choosing the Right Approach¶
Use Case |
API |
Note |
|---|---|---|
Binary serialization |
|
Default codec |
Binary deserialization |
|
Requires type + definitions |
JSON for web APIs |
|
REST/web integration |
Custom codec |
|
Raw, binary, token formats |
File I/O |
|
Low-level streaming |
Codec¶
A class used to instantiate a codec. |
|
An interface used to create encoder, decoder and sizer. |
Binary Streams¶
A class used to read data through the StreamRawReading interface (handle endianness). |
|
A class used to write data through the StreamRawWriting interface (handle endianness). |
|
A class used to read through the StreamRawReading interface (handle endianness). |
|
A class used to write through the StreamRawWriting interface (handle endianness). |
Stream I/O¶
A class used to read data from a blob. |
|
A class used to read data from a file. |
|
A class used to read data from a shared memory region. |
|
A class used to write data to a blob. |
|
A class used to write data to a file. |
|
A class used to write data to a shared memory region. |
Protocols¶
An interface used to read data. |
|
An interface used to read data. |
|
An interface used to write data to a blob. |
|
An interface used to read data from a blob. |
|
An interface used to get the size of data. |
Raw Streams¶
An class used to read data through the StreamRawReading interface. |
|
A class used to write data through the StreamRawWriting interface. |
|
An interface used to read data. |
|
An interface to write data. |