json
JSON syntax highlighter. Pure `(text) -> { Segment }`. Mirrors the algorithm of the deleted Rust `highlight_json` (renderer.rs): emits object keys via lookahead-for-`:`, handles `\` escapes inside strings, recognises numbers with scientific notation, and colors structural punctua…
json
JSON syntax highlighter. Pure (text) -> { Segment }. Mirrors the
algorithm of the deleted Rust highlight_json (renderer.rs): emits
object keys via lookahead-for-:, handles \ escapes inside strings,
recognises numbers with scientific notation, and colors structural
punctuation ({ } [ ] , :). Colors flow from ui.getToken("code.<role>")
via the active theme; hardcoded fallbacks match the deleted Rust values
when no theme is registered.
Exports
highlight(text: string) -> { Segment }— tokenize a JSON string into colored segments for the zui code renderer. The module returns this function directly.
Types:
Segment = { text: string, color: string, monospace: boolean }
Usage
local highlight = require("@builtin::modules.zui.highlight.json")
local segments = highlight('{"a": 1, "b": "two"}')
Notes
- Pure function — no side effects, no state. Safe to call any time.
- The
ui.getTokenlookup runs once per highlighter call (5 FFI calls) rather than per byte, so the inner tokenize loop is allocation-light. - Keys are disambiguated from string values by a lookahead past
whitespace for
:— matches the deleted Rust algorithm exactly.
Scoped to this part · feeds back into the world's score.