JsonTree
HkJsonTree is the interactive JSON viewer: give it a raw JSON string (text) or an already-parsed object (value), and it renders an expandable tree of the payload — one row per key, values tinted by type (null / boolean / number / string) with a type hint beside them, containers summarised in one clipped line while closed and collapsing to a {n} / Array(n) badge while open. Non-container input renders nothing at all, so the component can be fed any tool output without a guard.
- The general nested row view — arbitrary labels, icons and statuses rather than a JSON payload — is Tree; both share the toggle-chevron / indented-children grammar but nothing else.
- A JSON tree is read-only by default and denies text selection on purpose (see "Copy, selection and motion"); the payload is extracted through its copy affordance instead.
- Vue's editable mode (menu-driven key/value editing) is Vue-only; the mirror has no editing path — see "What the mirror does not have".
Import
;
use *; // JsonTree, JsonValue, JsonNode
use *; // JsonTreeProps, rsx!, VNode
The Rust form is a plain function, JsonTree(JsonTreeProps { … }). Only JsonTreeProps is in the prelude; JsonValue and JsonNode (and the json_* parser helpers) come from the crate root. The Vue barrel exports the component under both spellings — HkJsonTree and the aliased HJsonTree — plus buildJsonTree and the HJsonNode type; either name compiles.
Feeding it a payload
Vue reads value first and falls back to text; the text path runs JSON.parse and keeps only objects and arrays, so a bare scalar or a syntax error renders nothing. maxDepth (default 8) caps how deep the tree is built — deeper containers still render, but with no children under them.
The mirror takes text (parsed internally, containers only) or a pre-parsed value that wins over it, and implements its own RFC 8259 reader because the crate does not depend on serde_json. That reader accepts non-strict spellings JSON.parse rejects (1. and +1), keeps duplicate object keys in source order where JSON.parse keeps the last value and reorders integer-like keys first, errors on an overflow such as 1e999 where JSON.parse yields Infinity, and caps nesting at 128 levels.
Folding: the open set, and the mirror's controlled twin
Vue seeds the expansion set with the root plus its direct containers and resets it whenever the tree is rebuilt (fresh node ids); every later fold is internal state, and the component emits nothing about it — its one emit is update:value, which only the editable path uses.
The mirror takes the set as the expanded prop: None reproduces Vue's initial set (json_initial_expanded), and a toggle is reported through on_toggle with the node id. Ids are numbered after recursion (post-order) exactly as Vue numbers them, so a host that wants to pre-fold a specific node has to walk JsonNode.children rather than guess an index. A long string (more than 72 characters, JSON_STR_PREVIEW_LEN) is treated as a container for folding purposes: closed it shows its first 72 characters and a string (n) hint, open it shows one row per line.
The two snapshots above are stills: the Rust form draws the fold state it is handed and runs nothing.
Copy, selection and motion
A press on a leaf row copies the payload in Vue and fires on_copy with the raw JSON in the mirror (there is no clipboard on the wasm target). A press on a parent row toggles instead and never copies — the root's press handler skips any .s-jt-row[data-parent]. selectable opts a host into row text selection; without it the sheet denies selection so a click meant to toggle or copy cannot leave half a selected payload behind, and a selection drag that started inside an opt-in tree is preserved instead of firing its handler.
The mirror keeps the marker but not the motion: the chevron rotates from data-open in both forms, the height fold is Vue-only (a JS-measured transition; the tairitsu renderer has no transition machinery), and Vue additionally wraps every node in .s-jt-group in both states so its fold transition has a stable home. Vue's fold also honours reduced motion and the global animation switch by snapping instead of animating.
What the mirror does not have
- No editable. Vue's menu-driven editing (Editable in the API table) opens a context menu per row, flips it into an inline editor, and hands every commit back through update:value. The mirror has neither the prop nor the update:value emit: its only outbound signals are on_toggle and on_copy.
- No transition. Nothing folds over time on the Rust side; the final state is what renders.
- A different copy payload for value. Vue stringifies value with two-space indentation (JSON.stringify(value, null, 2)); the mirror serialises it compactly with json_serialize.
- A different render host. The root keeps the legacy s-tool-json-tree class because hosts target it, and it is bounded to a 320px max height with its own overlay scroller; the mirror compiles the same sheet, so the cap and the scrollbars match.
API
Vue
Props
| Name | Type | Default | Required |
|---|---|---|---|
| text | String | "" | — |
| value | null | undefined | — |
| maxDepth | Number | 8 | — |
| ariaLabel | String | "" | — |
| selectable | Boolean | false | — |
| editable | Boolean | false | — |
Emits
| Event | Payload |
|---|---|
| update:value | — |
Rust
Props — JsonTree
| Prop | Type | Default | Description |
|---|---|---|---|
| text | String | "" | Raw JSON text, parsed internally (containers only). |
| value | Option<JsonValue> | None | Pre-parsed value; takes precedence over text (Vue: value). |
| max_depth | usize | JSON_TREE_MAX_DEPTH (8) | Child depth to build (Vue default 8). |
| expanded | Option<Vec<usize>> | None | Controlled expansion set; None uses Vue's initial set. |
| aria_label | Option<String> | None | Accessible name for the tree role. |
| selectable | bool | false | Deliberate opt-in to row text selection (Vue selectable; the sheet denies selection by default). |
| on_toggle | Option<EventHandler<usize>> | None | A parent row was clicked (its node id). |
| on_copy | Option<EventHandler<String>> | None | A leaf row was clicked (the raw JSON text). |
| class | String | "" | — |
Accessibility
- The root is role="tree" and carries aria-label from ariaLabel / aria_label — an empty string writes no attribute in either form, so name the tree whenever there is more than one on a page.
- No row declares a tree item role. The rows are div.s-jt-row with no role="treeitem", no aria-level and no aria-expanded, so the announced structure is "a tree containing plain text" rather than a navigable item list; a screen reader gets the key/value text but not the position or the fold state. The open state is visual only (data-open, plus a rotated chevron).
- Nothing is focusable and no key is bound. There is no tabindex, no roving cursor and no keyboard fold in either form, so the whole viewer is pointer-only; a host that needs keyboard access drives expanded from its own controls.
- The copy affordance is a press on a leaf row with no button, label or announcement — a screen-reader user cannot reach it. If copying matters, put a real button beside the tree and call the same code path (on_copy in the mirror is the seam for this).
- Long strings and containers are clipped on purpose (.s-jt-clamp / .s-jt-preview): the collapsed text is not the full value, so never treat what is visible as the payload.
- Selection is denied by default, which also takes the value away from a reader who wants to copy a fragment; selectable is the deliberate opt-in and changes nothing that assistive technology announces.
- Colour is the only signal of a value's type (.s-jv-null / bool / num / str), so the type is also written as plain text beside the value (s-jt-type), which is the part that survives without a stylesheet.
Related components
- Tree — the general nested row view with labels, icons and statuses.
- TreeNode — the legacy Rust-only node: a single leaf with its own arrow / content / label markup.
- HkToolBlock — the chat tool-output pane that consumes this viewer for its JSON results.
- Collapse — the legacy animated fold, if a host wants a height transition the Rust JSON tree does not have.