Skip to content

Pretty-print JSON cells by default without marking them changed#1412

Merged
datlechin merged 10 commits into
mainfrom
fix/json-pretty-print-display
May 25, 2026
Merged

Pretty-print JSON cells by default without marking them changed#1412
datlechin merged 10 commits into
mainfrom
fix/json-pretty-print-display

Conversation

@datlechin
Copy link
Copy Markdown
Member

What

JSON and JSONB cells now display pretty-printed by default in every viewer (inline sidebar cards, expanded sidebar, cell popover, pop-out window), in both read-only and editable modes. Viewing or reformatting a value no longer marks the row as changed. Original key order and exact number values are preserved everywhere, including the Tree view and on save.

From user feedback: the old "Format JSON" button reformatted by rewriting the cell value, which marked the row dirty, and the formatter sorted keys and rounded large integers. For an event store that is JSONB everywhere, that made the everyday "just let me read this" action destructive.

Root cause

Four faults combined:

  • Pretty was modeled as a separate string only in read-only mode. The editable viewer bound the raw compact value, so the only way to indent was a button that overwrote the cell.
  • Change detection compared raw strings, so reindented whitespace read as a real edit.
  • The formatter round-tripped through JSONSerialization with .sortedKeys, which reorders keys and coerces numbers through NSNumber, losing precision above 2^53 (snowflake / bigint IDs).
  • Save re-compacted with .sortedKeys, reordering keys on every save.

Changes

  • New JsonSyntaxParser + JsonReindenter: a scalar-level parser and formatter that copies keys, number tokens, and string contents byte for byte. Preserves key order and exact numbers, handles top-level primitives, passes invalid JSON through unchanged, capped at 500 KB.
  • JSONViewerView now holds a displayText buffer that is pretty in both modes. Removed the {} Format button (the view opens pre-formatted). Save stores the normalized, order- and precision-preserving form.
  • MultiRowEditState.updateField compares JSON fields semantically (normalize(new) == normalize(original)), so a pure reformat is never a change. Added a cached isJson flag, also true for text columns whose value looks like JSON.
  • Tree view rewired through the same parser, so it keeps key order and number precision too.
  • Inline sidebar JSON cards show pretty-printed.
  • JSONSyntaxTextView read-only viewer gains the find bar (Cmd+F) and plain-text mode.

Tests

  • JsonReindenterTests: key order, large-integer and decimal precision, top-level primitives, invalid passthrough, idempotence, normalize equivalence, escape and surrogate-pair decoding, size cap.
  • MultiRowEditStateJsonTests: reformat is not a change, key reorder is a change, normalized storage, revert clears the change, large integers preserved, text column holding JSON, non-JSON exact comparison.
  • StringJsonTests: updated for key-order preservation and the ": " separator (the prior sorted-keys behavior is intentionally dropped).

Docs

  • Updated docs/features/json-viewer.mdx and added a CHANGELOG entry under Added.

@datlechin datlechin merged commit 66ec3a9 into main May 25, 2026
2 checks passed
@datlechin datlechin deleted the fix/json-pretty-print-display branch May 25, 2026 07:35
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