Read JSON into plain Clojure data, built on the Java library io.github.ralfspoeth/greyson.
Reading is a two-step process:
read-json / read-json-str parse text into Greyson's
immutable JsonValue tree.->clj converts that tree into maps, vectors, strings,
BigDecimals, booleans and nil.Example:
(->clj (read-json-str "{"a": [1, true, null]}")) ;; => {"a" [1M true nil]}
Read JSON into plain Clojure data, built on the Java library
io.github.ralfspoeth/greyson.
Reading is a two-step process:
1. `read-json` / `read-json-str` parse text into Greyson's
immutable `JsonValue` tree.
2. `->clj` converts that tree into maps, vectors, strings,
BigDecimals, booleans and nil.
Example:
(->clj (read-json-str "{\"a\": [1, true, null]}"))
;; => {"a" [1M true nil]}(->clj v)(->clj v key-fn)Converts the Greyson JsonValue v into plain Clojure data:
JSON object -> persistent map JSON array -> vector JSON string -> String JSON number -> BigDecimal (full precision, e.g. 1M, 0.1M) true / false -> Boolean null -> nil
key-fn is applied to every object member name (a String) to
produce the map key, at every level of nesting. It defaults to
identity, so keys stay strings. Common choices:
(->clj v) ;; {"a" 1M} (->clj v keyword) ;; {:a 1M} (->clj v #(keyword "my.ns" %)) ;; {:my.ns/a 1M}
If an object has duplicate member names, the last one wins (this
is Greyson's behaviour). If key-fn maps two different names to
the same key, the later member in iteration order wins, and that
order is unspecified.
Throws IllegalArgumentException if v is not a JsonValue.
Converts the Greyson `JsonValue` `v` into plain Clojure data:
JSON object -> persistent map
JSON array -> vector
JSON string -> String
JSON number -> BigDecimal (full precision, e.g. 1M, 0.1M)
true / false -> Boolean
null -> nil
`key-fn` is applied to every object member name (a String) to
produce the map key, at every level of nesting. It defaults to
`identity`, so keys stay strings. Common choices:
(->clj v) ;; {"a" 1M}
(->clj v keyword) ;; {:a 1M}
(->clj v #(keyword "my.ns" %)) ;; {:my.ns/a 1M}
If an object has duplicate member names, the last one wins (this
is Greyson's behaviour). If `key-fn` maps two different names to
the same key, the later member in iteration order wins, and that
order is unspecified.
Throws `IllegalArgumentException` if `v` is not a `JsonValue`.(read-json rdr)Reads exactly one JSON value from rdr and returns it as a
Greyson JsonValue. Closes rdr when done.
Any JSON value is accepted at the top level, including scalars
such as 42 or "text". Surrounding whitespace is ignored.
Throws:
java.util.NoSuchElementException if the input is empty or
contains only whitespace,io.github.ralfspoeth.json.io.JsonParseException if the input
is malformed or has anything after the first value,java.io.IOException if reading from rdr fails.Reads exactly one JSON value from `rdr` and returns it as a
Greyson `JsonValue`. Closes `rdr` when done.
Any JSON value is accepted at the top level, including scalars
such as `42` or `"text"`. Surrounding whitespace is ignored.
Throws:
- `java.util.NoSuchElementException` if the input is empty or
contains only whitespace,
- `io.github.ralfspoeth.json.io.JsonParseException` if the input
is malformed or has anything after the first value,
- `java.io.IOException` if reading from `rdr` fails.(read-json-str s)Reads exactly one JSON value from the string s and returns it
as a Greyson JsonValue. Throws the same exceptions as
read-json.
Reads exactly one JSON value from the string `s` and returns it as a Greyson `JsonValue`. Throws the same exceptions as `read-json`.
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 |