Fast JSON parser/writer working directly on UTF-8 bytes.
Fast JSON parser/writer working directly on UTF-8 bytes.
(parse input)(parse input {:keys [key-fn max-depth max-number-length bigdec]})Parses JSON from input (byte-array, String or InputStream) into Clojure
data structures. A leading UTF-8 byte order mark is skipped.
Options:
:key-fn - fn of String -> map key, applied to object keys.
Unset or nil leaves keys as strings. clojure.core/keyword is recognized
and uses an optimized interning path. Must be pure: results are cached by
fn identity, so a def'd fn gets cross-parse caching while a fresh lambda
is only cached within a single parse:max-depth - maximum nesting depth (default 1000):max-number-length - maximum length of a number in chars (default
1000): converting huge numbers is superlinear:bigdec - read non-integer numbers as exact BigDecimals
instead of doubles (default false)Throws com.s_exp.oda.JsonParseException on invalid input; its ex-data
is {:offset :line :column} (byte offset, 1-based line and column).
Parses JSON from `input` (byte-array, String or InputStream) into Clojure
data structures. A leading UTF-8 byte order mark is skipped.
Options:
* `:key-fn` - fn of String -> map key, applied to object keys.
Unset or nil leaves keys as strings. `clojure.core/keyword` is recognized
and uses an optimized interning path. Must be pure: results are cached by
fn identity, so a def'd fn gets cross-parse caching while a fresh lambda
is only cached within a single parse
* `:max-depth` - maximum nesting depth (default 1000)
* `:max-number-length` - maximum length of a number in chars (default
1000): converting huge numbers is superlinear
* `:bigdec` - read non-integer numbers as exact BigDecimals
instead of doubles (default false)
Throws `com.s_exp.oda.JsonParseException` on invalid input; its `ex-data`
is `{:offset :line :column}` (byte offset, 1-based line and column).(read reader)(read reader opts)Reads the next JSON value from reader (see reader).
Options:
:eof-value - returned when the input ends before another value
starts. Without it, the end of input throws java.io.EOFException.A value cut off by the end of input throws
com.s_exp.oda.JsonParseException, with positions counted from the
start of the stream.
Reads the next JSON value from `reader` (see `reader`). Options: * `:eof-value` - returned when the input ends before another value starts. Without it, the end of input throws java.io.EOFException. A value cut off by the end of input throws `com.s_exp.oda.JsonParseException`, with positions counted from the start of the stream.
(reader source)(reader source {:keys [key-fn max-depth max-number-length bigdec]})Returns a reader of successive JSON values from source (InputStream or
java.io.Reader), for read. Values may be separated by whitespace; each
read only pulls input until the next value's end, without waiting for
the end of the stream (a top-level number needs one byte of lookahead).
Input read past a value stays buffered in the reader, so read the stream
only through it. Closing it closes source. Not thread-safe.
Takes the options of parse, applied to every value.
Returns a reader of successive JSON values from `source` (InputStream or java.io.Reader), for `read`. Values may be separated by whitespace; each read only pulls input until the next value's end, without waiting for the end of the stream (a top-level number needs one byte of lookahead). Input read past a value stays buffered in the reader, so read the stream only through it. Closing it closes `source`. Not thread-safe. Takes the options of `parse`, applied to every value.
(write x out)(write x out {:keys [default-fn key-fn date-format max-depth encoders pretty]})Writes x as JSON to out (OutputStream). All bytes are handed to out
before returning, but out is neither flushed nor closed: flushing a
buffered out is the caller's (so many values can share one stream
without a flush each). See write-str for options.
Writes `x` as JSON to `out` (OutputStream). All bytes are handed to `out` before returning, but `out` is neither flushed nor closed: flushing a buffered `out` is the caller's (so many values can share one stream without a flush each). See `write-str` for options.
(write-bytes x)(write-bytes x
{:keys [default-fn key-fn date-format max-depth encoders pretty]})Writes x as JSON UTF-8 encoded byte-array. See write-str for options.
Writes `x` as JSON UTF-8 encoded byte-array. See `write-str` for options.
(write-str x)(write-str x {:keys [default-fn key-fn date-format max-depth encoders pretty]})Writes x as a JSON String.
Options:
:encoders - map of Class -> fn of value -> value to write, taking
precedence over built-in encodings (records, byte arrays, java.time...).
A value uses the encoder of its class, else of its nearest superclass,
else of its most specific interface, else of Object, like protocol
dispatch. The result is written without encoders (values nested inside
it do get them), and through :default-fn if not natively writable.
Map keys and primitive array elements don't go through encoders.
Resolutions are cached per map: reuse the same map across calls:default-fn - called on values that cannot be written natively — both
unsupported types AND supported types with unrepresentable values
(NaN/Infinity Float/Double, also as map keys). Must return a natively
writable value: it is not applied again to its own result (values nested
inside the result do get it). Without it, those cases throw.:key-fn - transforms each map key before dispatch. Use to handle
nil keys, custom types, or normalize keys. Takes the key, returns any
writable key (String, Keyword, Symbol, Number, UUID, Boolean, Character,
Enum).:date-format - format for java.util.Date and java.time.Instant values:
a pattern string or a java.time.format.DateTimeFormatter (zone-less
formatters default to UTC). Default: yyyy-MM-dd'T'HH:mm:ss'Z' at UTC.
Other java.time values are written as ISO-8601 strings, Java enums by
name.:max-depth - maximum nesting depth (default 1000); deeper or cyclic
values throw instead of overflowing the stack.:pretty - when true, indents output like Jackson's default pretty
printer (cheshire's and jsonista's :pretty): one object entry per
line, 2-space indent, "key" : value, arrays inline as [ 1, 2 ].Writes `x` as a JSON String. Options: * `:encoders` - map of Class -> fn of value -> value to write, taking precedence over built-in encodings (records, byte arrays, java.time...). A value uses the encoder of its class, else of its nearest superclass, else of its most specific interface, else of Object, like protocol dispatch. The result is written without encoders (values nested inside it do get them), and through `:default-fn` if not natively writable. Map keys and primitive array elements don't go through encoders. Resolutions are cached per map: reuse the same map across calls * `:default-fn` - called on values that cannot be written natively — both unsupported types AND supported types with unrepresentable values (NaN/Infinity Float/Double, also as map keys). Must return a natively writable value: it is not applied again to its own result (values nested inside the result do get it). Without it, those cases throw. * `:key-fn` - transforms each map key before dispatch. Use to handle nil keys, custom types, or normalize keys. Takes the key, returns any writable key (String, Keyword, Symbol, Number, UUID, Boolean, Character, Enum). * `:date-format` - format for java.util.Date and java.time.Instant values: a pattern string or a java.time.format.DateTimeFormatter (zone-less formatters default to UTC). Default: yyyy-MM-dd'T'HH:mm:ss'Z' at UTC. Other java.time values are written as ISO-8601 strings, Java enums by name. * `:max-depth` - maximum nesting depth (default 1000); deeper or cyclic values throw instead of overflowing the stack. * `:pretty` - when true, indents output like Jackson's default pretty printer (cheshire's and jsonista's `:pretty`): one object entry per line, 2-space indent, `"key" : value`, arrays inline as `[ 1, 2 ]`.
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 |