Skip to content

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

ParameterTypeDescription
modelunknownA 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 .rosetta format after programmatic edits
  • Generating a stub .rosetta file 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 RosettaFunction or RosettaRule elements — 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.
  • Condition expression bodies are emitted as True placeholder text — they are not serialized from the AST expression tree.
  • The model parameter uses unknown typing (duck typing) to avoid coupling to generated Langium types. Pass a RosettaModel AST node obtained from parse().

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)

Core packages released under MIT. Studio app released under FSL-1.1-ALv2.