Data normalization for TOON encoding.
Converts Clojure data structures to JSON-compatible values:
Data normalization for TOON encoding. Converts Clojure data structures to JSON-compatible values: - Keywords → strings - Symbols → strings - Sets → vectors - Maps with keyword keys → maps with string keys - UUIDs → strings - Dates/Instants → ISO-8601 strings - NaN/Infinity → nil - Functions/vars → nil - Objects implementing ToJson protocol → custom serialization
(normalize-value value)(normalize-value value depth max-depth)Normalizes a Clojure value to a JSON-compatible representation.
Normalization rules (in order of precedence):
Parameters:
Returns: JSON-compatible value (nil, boolean, number, string, vector, or map)
Throws: ex-info if max-depth is exceeded to prevent stack overflow
Normalizes a Clojure value to a JSON-compatible representation. Normalization rules (in order of precedence): - nil → nil - ToJson protocol → calls -to-json and normalizes result - Booleans → unchanged - Strings → unchanged - Numbers: - Finite → unchanged (with -0 → 0) - NaN, Infinity, -Infinity → nil - Keywords → strings (e.g., :foo → "foo", :user/id → "user/id") - Symbols → strings (e.g., 'foo → "foo") - UUIDs → strings - Dates/Instants → ISO-8601 strings - BigInt/BigDecimal → number or string (if out of safe range) - Sets → vectors (sorted for determinism) - Maps → maps with string keys (recursively normalized values) - Vectors/Lists → vectors (recursively normalized) - Functions, vars, undefined → nil Parameters: - value: Any Clojure value - depth: (optional) Current nesting depth (default: 0) - max-depth: (optional) Maximum nesting depth (default: 1000) Returns: JSON-compatible value (nil, boolean, number, string, vector, or map) Throws: ex-info if max-depth is exceeded to prevent stack overflow
Protocol for custom TOON/JSON serialization.
Objects implementing this protocol can control how they are serialized during TOON encoding. The -to-json method is called before any other type-specific normalization.
Example: (defrecord Person [first-name last-name] ToJson (-to-json [_] {:name (str first-name " " last-name)}))
(encode (->Person "Alice" "Smith")) ;; => "name: Alice Smith"
Protocol for custom TOON/JSON serialization.
Objects implementing this protocol can control how they are serialized
during TOON encoding. The -to-json method is called before any other
type-specific normalization.
Example:
(defrecord Person [first-name last-name]
ToJson
(-to-json [_]
{:name (str first-name " " last-name)}))
(encode (->Person "Alice" "Smith"))
;; => "name: Alice Smith"(-to-json this)Returns the JSON-compatible representation of this value.
Returns the JSON-compatible representation of this value.
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 |