Zipper-based navigation and editing API for JSON/JSONC node trees.
Navigation functions (right, left, down, up) skip whitespace and comment nodes by default, while preserving them in the tree.
Usage: (require '[rewrite-json.zip :as z]) (require '[rewrite-json.parser :as parser])
(-> (z/of-string "{"name": "old"}") (z/find-key "name") z/right (z/replace (node/string-node "new")) z/root-string) ;; => "{"name": "new"}"
Zipper-based navigation and editing API for JSON/JSONC node trees.
Navigation functions (right, left, down, up) skip whitespace and
comment nodes by default, while preserving them in the tree.
Usage:
(require '[rewrite-json.zip :as z])
(require '[rewrite-json.parser :as parser])
(-> (z/of-string "{\"name\": \"old\"}")
(z/find-key "name")
z/right
(z/replace (node/string-node "new"))
z/root-string)
;; => "{\"name\": \"new\"}"(append-entry loc key-name value-node)Append a new key-value entry to an object.
loc should be positioned at an object node.
Tries to match the existing formatting style.
Append a new key-value entry to an object. `loc` should be positioned at an object node. Tries to match the existing formatting style.
(entry-value entry-loc)Given a zipper positioned at an entry node, return a zipper positioned at the entry's value node (the first value-node after the colon).
Given a zipper positioned at an entry node, return a zipper positioned at the entry's value node (the first value-node after the colon).
(find-key loc key-name)From the current location (which should be an object or positioned
inside one), find the entry whose key matches key-name (string).
Returns the zipper positioned at the entry node, or nil.
Note: clojure.zip/down on an empty children seq destructures to a nil node; we guard against that explicitly in the loop.
From the current location (which should be an object or positioned inside one), find the entry whose key matches `key-name` (string). Returns the zipper positioned at the entry node, or nil. Note: clojure.zip/down on an empty children seq destructures to a nil node; we guard against that explicitly in the loop.
(pretty-value-node value-node container-loc)Convert value-node (already a node, typically from node/value->node)
to a multi-line formatted node, using container-loc (a zipper loc
positioned at the object/array that will contain the value) to infer
the indentation level.
Uses infer-indent-with-context so that even freshly created empty
containers inherit multi-line style from their ancestors.
When the container uses single-line formatting, or when value-node is
a scalar, it is returned unchanged.
Convert `value-node` (already a node, typically from `node/value->node`) to a multi-line formatted node, using `container-loc` (a zipper loc positioned at the object/array that will contain the value) to infer the indentation level. Uses `infer-indent-with-context` so that even freshly created empty containers inherit multi-line style from their ancestors. When the container uses single-line formatting, or when `value-node` is a scalar, it is returned unchanged.
(remove loc)Remove the node at the current location from its parent container. Handles comma cleanup for objects and arrays.
Remove the node at the current location from its parent container. Handles comma cleanup for objects and arrays.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |