Skip to content

feat(builder): JSON format round before customer definitions are stored #293

Done

Description

@vaceslav

Part of epic #261, phase 4 step 3 (see docs/superpowers/specs/2026-11-26-builder-direction-review.md on feature/builder). Must land before any customer JSON definition is stored: after that, every change is a migration.

Scope

  • Theme tokens: colours (and other theme values) as references ("$accent") to a theme section the host can override; round trip from C# theme objects.
  • Named assets: an assets map in the definition instead of sha256:… in the body; images with only a width or only a height.
  • Strict reader: unknown value formats ({{x:N2}}), padding with four values, a loop variable shadowing the data root, a text key missing in one language, markdown in literal text — errors (or warnings) instead of silent acceptance.
  • Renames for clarity (JSON and C#): textStyle/headerTextStyle → paragraphStyle/headerParagraphStyle, defaults.paragraph → defaults.paragraphStyle, cell align → verticalAlign, list if uses then like every other conditional.
  • texts by key: "title": {"de": …, "en": …}, so one wording change is one edit.
  • Named sections: {"section": "items", "blocks": […]} and c.Section(…) instead of group/from, as the unit for reading, replacing and diffing.
  • Cells: a style option; no ambiguous bare-array form.
  • Layout and small decisions: width counted from the indentation; \n/\t in text; reserved root keys (theme, assets, components, $schema).
  • C# API consistency: one position for the style parameter, one chaining rule.

Activity

  1. added
    enhancement
    builder
    on Nov 26, 2026
  2. vaceslav commented on Nov 26, 2026

    @vaceslav
    ContributorAuthor

    Additional scope from the real-world port (#292):

    F4 — Values that depend on the language but are not texts break the multi-language JSON export · significant for JSON

    The definition measures column widths against each language's vocabulary (the widest label and value per column), so
    German and English get different widths. TemplateJson.Export with TextLanguages = [de, en] then fails:

    TB9204: The JSON would not build the same template for en: a text is changed by code (string.Format, ToUpper,
    concatenated into a quoted value) or used where the JSON cannot hold it.
    

    Two problems: the JSON has no place for a per-language length, and the message blames a text although the difference
    is a column width. The port exports with one set of widths for both languages (an option of the definition) or one
    JSON per language.

    Related: TemplateTexts gives only the build language. Measuring against every language needs the catalog passed to
    the definition separately.

    template.Texts.In(CultureInfo.GetCultureInfo("en"))["label"];   // another language's text
    "columns": {"de": ["83.9pt", "57.25pt", "79.15pt", "84.4pt", 1], "en": ["98.45pt", "67.55pt", "77.15pt", "71.45pt", 1]}

    And TB9204 should name what differs: the first differing part and element.

    F5 — Components cannot reach texts or a theme · significant for JSON, cosmetic for C#

    ITemplateComponent.Compose(BlockComposer) sees neither the texts nor anything the definition shares. Every component
    of the port therefore takes the theme and the texts in its constructor. That works in C#, but such a component cannot
    be registered in a ComponentCatalog (its parameters are not paths or simple values), so the JSON export writes every
    use as an expanded copy: 58 group blocks in this template, the block of an organisation three times, one status box
    five times.

    public void Compose(BlockComposer c)
    {
        TemplateTexts t = c.Texts;                 // the build's texts
        BrandTheme theme = c.Shared<BrandTheme>(); // what the definition registered with template.Share(theme)
    }

    With that, a component's parameters are paths again and the catalog can write it as a reference.

    F6 — No reusable inline fragments · cosmetic

    Components are blocks. What repeats inside a paragraph (an icon followed by a word in a status colour; an address line
    with guarded separators) is a C# extension method on InlineComposer in the port. Fine in C#, invisible to JSON.

    public interface IInlineComponent { void Compose(InlineComposer x); }
    x.Component(new StatusMark(Status.Success, label));

    F12 — The exported JSON is mostly indentation · cosmetic

    The export of this template is 148,705 characters, of which 93,138 (63%) are leading spaces: components are nested
    groups and the tree is up to 35 levels deep. Minified it is 50,542 characters. For a file meant to be read and
    edited by an agent, an export option without indentation would cut the cost by two thirds.

    new TemplateJsonExportOptions { Indent = JsonIndent.None }
  3. vaceslav commented on Nov 26, 2026

    @vaceslav
    ContributorAuthor

    Done on feature/builder (commits referencing #293, up to 4fbc415). The issue stays open until the integration PR.

    Format (decisions A1–A9 of the style review)

    • Strict reader: only known value formats (host formats via TemplateJsonImportOptions.ValueFormats), padding with one or two values, a loop variable may not shadow its own collection's root, every text in texts has the languages of the others (a regional language is covered by its parent), == is read as =, $schema ignored, components reserved. TB9204 names the first paragraph that differs.
    • Names: then in every conditional, elseIf: [{if, then}], paragraphStyle / headerParagraphStyle, defaults.paragraphStyle, cell verticalAlign.
    • Structure: texts key first, {"section": …, "blocks": …} (C# c.Section), cells are a string or an object, \n / \t in strings.
    • Named assets (assets at the root, ImageSource.WithName), images with one size.
    • Theme tokens: theme.colors / theme.fonts, "$accent" wherever a colour or font goes (also in borders and in registered components' parameters), host override TemplateJsonImportOptions.Theme (a colour token needs a colour; an unknown token warns, TB9205).
    • Components see the build's texts and shared objects (c.Texts, c.Shared<T>(), template.Share, TemplateJsonImportOptions.Shared); a parameterless component is {"component": "name"}; texts a registered component reads go into the JSON's texts.
    • Layout width counted without indentation; TemplateJsonExportOptions.Indented = false writes one line.

    C#: the style parameter comes first everywhere (Text(style, text), Value(style, path[, format])); methods that add content return their composer, so short parts chain.

    Docs and sample: "JSON definitions" in the developer docs is now a reference (API, root, content, values, strictness, texts, sections/components/assets/theme). The sample uses theme tokens, a named logo, sections, chaining and a parameterless component that reads shared objects and texts; its JSON round trip is byte-identical in DOCX and ODT, de and en.

    Checks

    • A cheap model (one attempt, no validator) made 8 of 8 typical customer edits correctly on the new format: texts, a new table column, a row condition, moving a section, a new conditional box, a heading style, the brand colour (one theme value) and a component's text.
    • A final independent review found 6 problems, all fixed with tests: line breaks composed two ways, host value formats on export, incomplete TextLanguages, host theme values, tokens in component parameters, generic component names. Also fixed: two malformed-input cases that now give TB9200. Deferred minors: some composer-rejected values (e.g. a shadowing loop variable caught at build) surface at build rather than import; narrowing TextLanguages on re-export needs the build language; CellComposer options must come before content.
  4. vaceslav commented on Dec 2, 2026

    @vaceslav
    ContributorAuthor

    Done on feature/builder, merged to main in #316 and released in builder 0.1.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    builderenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions