/codec/json
Fast bidirectional JSON serialization (encode, decode, from-json) and derivable To-JSON / From-JSON support.
Traits#
From-JSON#
traitTrait allowing structs and types to be hydrated from decoded JSON data via `(derive My-Struct From-JSON)` or custom `(impl From-JSON My-Struct ...)`.
| Method | Signature | Description |
|---|---|---|
from-json | | — |
To-JSON#
traitTrait allowing types and structs to override default field-by-field JSON serialization.
| Method | Signature | Description |
|---|---|---|
to-json | | — |
Functions & symbols#
decode#
Parses a JSON text string or Slab byte buffer into native Tangu Maps, Lists, Numbers, Strings, Booleans, or `()`. When a `Target-Type` is provided as the second argument, hydrates the decoded data into the specified Struct or compound type via `From-JSON`. Returns an Error if malformed.
| Parameter | Type | Optional | Description |
|---|---|---|---|
json-string | String | required | JSON formatted text or Slab to parse |
target-type | Type | required | Optional struct or compound type to hydrate into |
Any(use /codec/json)
(let data (json/decode "{\"id\": 1, \"name\": \"Alice\"}"))
(assert-eq! (data :name) "Alice")(use /codec/json)
(def User (Struct #Int id #String name))
(derive User From-JSON)
(let u (json/decode "{\"id\": 1, \"name\": \"Alice\"}" User))encode#
Serializes a Tangu Map, List, Number, String, Boolean, or Struct instance into a JSON formatted string. Optional struct fields set to `()` serialize as null.
| Parameter | Type | Optional | Description |
|---|---|---|---|
expr | Any | required | Data structure, Map, List, or Struct instance to serialize |
Any(use /codec/json)
(let json-str (json/encode {:name "Bob" :scores [95 99]}))from-json#
Hydrates a decoded JSON Map or value into an instance of `Target-Type`, recursively converting nested struct fields, arrays, and optional fields.
| Parameter | Type | Optional | Description |
|---|---|---|---|
target-type | Type | required | Struct or compound type descriptor |
data | Any | required | Decoded JSON Map or value to hydrate |
Any(use {From-JSON from-json} /codec/json)
(def Product (Struct #Int id #String title))
(derive Product From-JSON)
(let p (from-json Product {:id 42 :title "Book"}))