rune-langium / core/src / serializeModel
Function: serializeModel()
ts
function serializeModel(model): string;Defined in: packages/core/src/serializer/rosetta-serializer.ts:294
Serialize a single RosettaModel AST node back to .rosetta source text.
Parameters
| Parameter | Type | Description |
|---|---|---|
model | unknown | A RosettaModel AST node (or duck-typed equivalent). |
Returns
string
Formatted .rosetta source text ending with a trailing newline.
Remarks
This serializer performs a lossy round-trip — it re-formats output with consistent indentation and does not preserve the original whitespace, comments, or annotation ordering. It is intended for code-generation and export workflows, not for preserving user-authored formatting.
Supported top-level element types:
Data(type definitions)Choice(choice types)RosettaEnumeration(enum types)
Unsupported elements (functions, rules, reporting rules, annotations) are silently skipped. Condition expression bodies are emitted as True placeholders.
Use When
- Exporting a modified AST back to
.rosettaformat after programmatic edits - Generating a stub
.rosettafile from a synthesized model object - Round-trip testing: parse → mutate → serialize → re-parse
Avoid When
- You need to preserve user-authored comments or whitespace — use a CST-preserving formatter instead.
- The model contains
RosettaFunctionorRosettaRuleelements — these are silently dropped; use the visual editor serializer for full round-trip fidelity.
Pitfalls
- Output does NOT include function or rule bodies — function/rule elements are skipped entirely. Do not use for models where function definitions are critical.
Conditionexpression bodies are emitted asTrueplaceholder text — they are not serialized from the AST expression tree.- The
modelparameter usesunknowntyping (duck typing) to avoid coupling to generated Langium types. Pass aRosettaModelAST node obtained fromparse().
Example
ts
import { parse, serializeModel } from '@rune-langium/core';
const { value } = await parse(source);
const text = serializeModel(value);
// text is valid .rosetta source (minus functions/rules/comments)