Skip to content

The model file format

Audience: people who read files in git or edit them outside the IDE · ~8 min read · Prerequisites: Project folder anatomy

What it is

A model is stored as ordinary text files you can open in any editor. The program itself is real source text in a model file (.model.py or .model.js). Everything that is not program text sits in two small JSON files: model.json beside the model file and project.json at the top of the project. The text is parsed back into the tree every time the file opens, so the file on disk is the single source of truth.

When you'd use it

  • You review a model in a pull request and want to read the diff.
  • You generate or fix a model with a script or another tool.
  • Something went wrong on load and you need to know which file to repair.

The model file

The extension names the grammar the text is written in:

File Grammar
name.model.py Python syntax (the default)
name.model.js JavaScript syntax, for languages that choose it
name.model.json, name.model.xml Languages whose own format is JSON or XML (see Bundled languages)

The compound extension lets struktJS tell its own files from ordinary .py and .js files in the same folder.

A model file may begin with header lines, then a blank line, then the program:

# modelimports: shared-utils
# langversion: 3

def to_fahrenheit(celsius):
  return celsius * 1.8 + 32
Header line Meaning
# package: x.y The model's package prefix for qualified names
# modelimports: a, b Other models this model may refer into
# format: javascript Written when the grammar is not Python, so the file reads back correctly
# langversion: N Version of the language the file was saved with; used to migrate old files

Older files may still have # name:, # label: or # description: lines. They are read and dropped the next time the file is saved. A model's name is its folder and file name; its label and description live in model.json. Languages whose format cannot carry a header (JSON, XML) keep package and langVersion in model.json instead.

What the program text looks like

Concepts that have a natural spelling in the grammar are written that way (def, for, if). A concept without one is written as a plain function call named after the concept, for example decisionTable("F", decisionRow(score >= 90, "A")) or printFormula(fraction(8, 4)). Use Surface in the Surface pane to see exactly what is stored.

Marker comments

Some features are stored as comments that keep the file readable in any tool. Treat them as part of the file; do not reword them.

Comment Meaning
# struktjs:doc <line> and # struktjs:doc[path] <line> Documentation attached to the statement below (or to a node inside it). // in JavaScript-grammar files
# struktjs:suppress rule1 rule2 Silences those checks on the statement below
# @ignore(rule1, rule2) Read as the same marker (saved back as struktjs:suppress); @ignore alone silences every check; an at: path is not supported
# struktjs:disabled:begin … # struktjs:disabled:end A block of commented-out code kept as one "disabled" unit; each line in between is the frozen original line

model.json

Field Meaning
imports Languages the model uses: [{ "qualifiedName": "core.py", "alias": "..." }]. The first is the primary language
modelImports Qualified names of other models this model may point into
label, description Display name and description
roots Display name of each root file, keyed by file name
primaryRoot File name of the primary root
rootLanguages Root file name → qualified name of that root's own language, for a root that is not in the model's primary language. A root without an entry follows the primary. Each such root loads, opens and saves in its own language; the model's other imports stay available to every root (see the Mixed-language roots sample)
output Where Generate writes files (default: a generated/ folder in the model folder)
package, langVersion Only for formats with no header (JSON, XML)
generatable false marks a sample as diagnostic-only: Generate is disabled and the sample picker shows a "diagnostic-only" badge

A root file name in primaryRoot, roots or rootLanguages that matches no root file in the model's folder is reported when the project opens (a load error naming the field it came from). Fix the name in model.json or restore the file; the model opens with the roots it does have.

The roots of a model are not listed in model.json: they are detected from the model files in the model's folder, so the Model Properties dialog only renames them (its Additional roots section). To add a root, use Add root node in the Explorer. The raw JSON box in the dialog is syntax-highlighted.

Example from the Multi-root model sample:

{
  "imports": [{ "qualifiedName": "core.py" }],
  "label": "Primary Program",
  "roots": {
    "multi-root-demo.model.py": "Primary Program",
    "temperature-stats.model.py": "Temperature statistics (second root)"
  }
}

project.json

{ "name": "...", "description": "..." }. Bundled samples add "group" (the heading the sample appears under in the Load Sample picker: diagram, feature, python, or anything a plugin adds; no group means "General samples").

Other files you will meet

File Purpose
*.lang.js A language defined by the project. It is code that runs: see Trusting project code
python-stubs/*.pyi Python type stubs used for completion
versions.json Saved versions of a root (Tools > Versions); commit it to share them
generated/ Default generator output; safe to delete

What is safe to hand-edit

Safe Be careful Do not
The program text, if it still parses modelimports and model.json imports (a wrong name makes the model unresolvable) Rename a model file to a name without .model.<ext>
label, description, roots display names, output The marker comments above Delete model.json while models still use its imports
project.json name and description Renaming a model folder (imports that name it by qualified name will point at nothing) Rename the model.json file (the name is fixed)

If you edit while the IDE has the model open, the model reloads when the file changes.

When the text does not parse

If a model file only parses partly, the IDE reports "Syntax error: the file could not be parsed completely, so code around the error is not shown. Saving this file is disabled until the error is fixed (fixing it on disk reloads it)." The file is left exactly as it is. Fix the text in an external editor and save; the model reloads. If project.json is damaged (not a JSON object, empty or invalid), the project opens read-only and the error names the problem. A model.json with a leading byte-order mark is accepted.

Every control

# Button/Option What it does Shortcut Gotcha
1 Open project.json (Explorer header menu) Opens project.json as text none Needs a real folder
2 Organize > Edit properties… Edits model.json through a form, with a raw JSON box none Safer than hand-editing
3 File > Export > Export as Model Surface Text Writes the Surface text out Ctrl/⌘+K, F, M
4 Open language.json (language row) Opens a language's sidecar file none Not for project-owned languages