YAML Serialization
YAML is a human‑readable format suited for model definitions and example data. Maryk supports YAML for both models and data.
What You Can Serialize
Section titled “What You Can Serialize”- Models: author and exchange DataModel definitions (schemas). See Data Models.
- Data: serialize/deserialize values for any model.
- Requests & Responses: encode
Requestsenvelopes and typed responses (e.g.,ValuesResponse) to interact with stores.
Why YAML
Section titled “Why YAML”- Easy to read and review in pull requests.
- Supports anchors, aliases, comments, and complex keys used by Maryk’s definitions.
Parsers and Writers
Section titled “Parsers and Writers”- Maryk provides its own streaming YAML module to ensure consistent behavior across platforms and to support required features.
- Writer:
maryk.yaml.YamlWriter - Reader:
maryk.core.yaml.MarykYamlReader - Module docs: YAML module
- Writer:
Model Definition Example
Section titled “Model Definition Example”name: Userkey:- !Ref username? 0: username: !String { required: true, final: true, unique: true }? 1: email: !String { required: true, unique: true }Data Example
Section titled “Data Example”username: john.smithemail: john.smith@gmail.comSerialize Values to YAML
Section titled “Serialize Values to YAML”val out = StringBuilder()User.writeYaml(userValues, YamlWriter { out.append(it) })Deserialize Values from YAML
Section titled “Deserialize Values from YAML”val it = yamlString.iterator()val reader = MarykYamlReader { if (it.hasNext()) it.nextChar() else Char.MIN_VALUE }val values = User.readYaml(reader)Interoperability
Section titled “Interoperability”- YAML works well for human‑authored content and demos. For production transport, prefer ProtoBuf or JSON.
Related
Section titled “Related”Requests & Responses
Section titled “Requests & Responses”YAML can also carry store requests and responses. Use the Requests envelope to send multiple operations in one payload, and provide a RequestContext so names and references resolve correctly.
Example: Serialize a Get request
Section titled “Example: Serialize a Get request”// Build a request: get one user by key and only select nameval req = Requests( User.get( keys = listOf(userKey), select = User.graph { listOf(name) } ))
// Build a context with known modelsval defs = DefinitionsContext().apply { dataModels["User"] = DataModelReference(User)}val ctx = RequestContext(defs)
// Write YAMLval out = StringBuilder()Requests.writeYaml(req, YamlWriter { out.append(it) }, ctx)Example: Read a ValuesResponse from YAML
Section titled “Example: Read a ValuesResponse from YAML”val it = yamlString.iterator()val reader = MarykYamlReader { if (it.hasNext()) it.nextChar() else Char.MIN_VALUE }val ctx = RequestContext(DefinitionsContext(mutableMapOf("User" to DataModelReference(User))))
// Parse a typed responseval response = ValuesResponse.readYaml(reader, ctx)Notes
- For requests/responses, always pass a
RequestContextso model and reference names resolve during parsing. - You can combine multiple operations (Get/Scan/Add/Change/Delete/Collect) in a single
Requestspayload.