Skip to content

Add a JSON formatter, the reverse of %json - #12

Merged
Demonstrandum merged 2 commits into
masterfrom
json-formatter
Nov 6, 2026
Merged

Demonstrandum merged 2 commits into
masterfrom
json-formatter

Conversation

@Demonstrandum

@Demonstrandum Demonstrandum commented Nov 4, 2026 •

Copy link
Copy Markdown
Owner

Adds JSON output, with --json and (%formatter/json ...).

  • A list of :key value attributes is written as an object, and any other list as an array. A list mixing the two is an error, pointing at the first odd item.
  • Strings are strings and numbers are numbers (0x10 → 16). The symbols true, false and null are themselves, and other symbols are strings.
  • A file's top level is one value if it is a single value, or else the items of a list.
  • A document is indented. %formatter/json is compact unless given :document true.
  • </ in strings is written <\/, so JSON can't close a <script> it is embedded in.

%json/%yaml keep types. Unquoted numbers, true/false and null used to be read as strings ((:a "1")). They are now numbers and symbols, so reading JSON in and writing it back out gives the same JSON. Quoted values stay strings. This also changes YAML frontmatter: draft: true is now the symbol true. The blog sample's output is byte-for-byte identical.

Scripts. Writing JavaScript as seam source is fragile: ; starts a comment, and strings were written back with Rust's escapes. So now:

  • A <script> whose only content is a string (e.g. """...""") is written as is, like <style> already was.
  • Otherwise, strings inside a script are written as JavaScript string literals, and raw text (like JSON) as is. Everything else is still copied as written. samples/tests/html-test-2.sex renders the same.
  • The docs recommend giving data to scripts as a block, (script :type application/json :id posts (%formatter/json %posts)), read with JSON.parse.

Blog. Adds feed.json.sex, a JSON Feed written as s-expressions. It's built by build.sex and linked from every page.

Tests cover the formatter, round-tripping %json, typed YAML scalars and JSON inside a script.


Note

Medium Risk
Changes affect HTML script emission and YAML/JSON parsing semantics site-wide; existing templates relying on string-typed booleans or old script quoting could behave differently.

Overview
Adds JSON output via --json and (%formatter/json ...), mapping s-expressions back to JSON (attribute lists → objects, other lists → arrays) with compact output by default and pretty printing when :document true. String escaping includes <\/ after < so JSON embedded in <script> tags cannot break out.

%json and %yaml now preserve scalar types (numbers, true/false/null) instead of treating unquoted values as strings, enabling round-trip JSON and changing how YAML frontmatter booleans behave (e.g. draft: true is the symbol true).

HTML <script> generation is updated: a single string child is emitted verbatim; structured script content keeps seam syntax but escapes string nodes as JavaScript/JSON string literals via the shared write_string helper.

The blog sample gains a JSON Feed (feed.json.sex), build step, and application/feed+json alternate link. Docs and tests cover the formatter, round-trips, typed YAML, and JSON-in-script cases.

Reviewed by Cursor Bugbot for commit a93c4ce. Bugbot is set up for automated code reviews on this repo. Configure here.

--json and %formatter/json write a list of attributes as an object, any
other list as an array, and true, false and null as themselves. %json
and %yaml now read unquoted numbers, booleans and null as numbers and
symbols instead of strings, so JSON read in is written back the same.
Raw text inside a <script> is written as is, so JSON can be given to a
script, and the blog sample gains a JSON Feed.
Writing JavaScript as seam source is fragile, since ; starts a comment,
and strings were written back with Rust's escapes. A script that is a
single string is now written as it is, as with style, and otherwise
strings in a script are written as JavaScript strings. JSON is best
given to scripts as a data block, which is what the docs now show.
@Demonstrandum
Demonstrandum merged commit a0c7464 into master Nov 6, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant