JSON encoding and decoding based on Jackson Databind.
Encoding example:
(require '[jsonista.core :as json])
(json/write-value-as-string {:hello 1})
;; => "{\"hello\":1}"
Decoding example:
(def +data+ (json/write-value-as-string {:foo "bar"}))
(json/read-value +data+)
;; => {"foo" "bar"}
You can configure encoding and decoding by creating a custom mapper object with jsonista.core/object-mapper. The options are passed in as a map.
For example, to convert map keys into keywords while decoding:
(json/read-value +data+ (json/object-mapper {:decode-key-fn true}))
;; => {:foo "bar"}
See the docstring of object-mapper
for all available options.
Custom encoder is a function that take a value and a JsonGenerator object as the parameters. The function should call JsonGenerator methods to emit the desired JSON. This is the same as how custom encoders work in Cheshire.
Custom encoders are configured by the object-mapper option :encoders, which is a map from types to encoder functions.
For example, to encode java.awt.Color:
(let [encoders {java.awt.Color (fn [color gen] (.writeString gen (str color)))}
mapper (json/object-mapper {:encoders encoders})]
(json/write-value-as-string (java.awt.Color. 1 2 3) mapper))
;; => "\"java.awt.Color[r=1,g=2,b=3]\""
jsonista uses Jackson Databind while Cheshire uses Jackson Core. In our benchmarks, jsonista performs better than Cheshire (take look at json_perf_test.clj). On the other hand, Cheshire has a wider set of features and has been used in production much more.
JSON encoding and decoding based on Jackson Databind. Encoding example: (require '[jsonista.core :as json]) (json/write-value-as-string {:hello 1}) ;; => "{\"hello\":1}" Decoding example: (def +data+ (json/write-value-as-string {:foo "bar"})) (json/read-value +data+) ;; => {"foo" "bar"} ## Configuration You can configure encoding and decoding by creating a custom mapper object with jsonista.core/object-mapper. The options are passed in as a map. For example, to convert map keys into keywords while decoding: (json/read-value +data+ (json/object-mapper {:decode-key-fn true})) ;; => {:foo "bar"} See the docstring of [[object-mapper]] for all available options. ## Custom encoders Custom encoder is a function that take a value and a JsonGenerator object as the parameters. The function should call JsonGenerator methods to emit the desired JSON. This is the same as how custom encoders work in Cheshire. Custom encoders are configured by the object-mapper option :encoders, which is a map from types to encoder functions. For example, to encode java.awt.Color: (let [encoders {java.awt.Color (fn [color gen] (.writeString gen (str color)))} mapper (json/object-mapper {:encoders encoders})] (json/write-value-as-string (java.awt.Color. 1 2 3) mapper)) ;; => "\"java.awt.Color[r=1,g=2,b=3]\"" ## Jsonista vs. Cheshire jsonista uses Jackson Databind while Cheshire uses Jackson Core. In our benchmarks, jsonista performs better than Cheshire (take look at json_perf_test.clj). On the other hand, Cheshire has a wider set of features and has been used in production much more.
DEPRECATED: The default ObjectMapper instance.
DEPRECATED: The default ObjectMapper instance.
The default ObjectMapper instance.
The default ObjectMapper instance.
ObjectMapper instance that uses keyword keys for maps
ObjectMapper instance that uses keyword keys for maps
(object-mapper)
(object-mapper options)
Create an ObjectMapper with Clojure support.
The optional first parameter is a map of options. The following options are available:
Mapper options | |
---|---|
:modules | vector of extra ObjectMapper modules |
:factory | A Jackson JsonFactory for this given mapper |
:mapper | The base ObjectMapper to start with - overrides :factory |
Encoding options | |
---|---|
:order-by-keys | set to true to order map keys alphabetically |
:pretty | set to true use Jacksons pretty-printing defaults |
:escape-non-ascii | set to true to escape non ascii characters |
:strip-nils | remove any keys that have nil values |
:date-format | string for custom date formatting. default: yyyy-MM-dd'T'HH:mm:ss'Z' |
:encode-key-fn | true to coerce keyword keys to strings, false to leave them as keywords, or a function to provide custom coercion (default: true) |
:encoders | a map of custom encoders where keys should be types and values should be encoder functions |
Encoder functions take two parameters: the value to be encoded and a JsonGenerator object. The function should call JsonGenerator methods to emit the desired JSON.
Decoding options | |
---|---|
:decode-key-fn | true to coerce keys to keywords, false to leave them as strings, or a function to provide custom coercion (default: false) |
:bigdecimals | true to decode doubles as BigDecimals (default: false) |
Create an ObjectMapper with Clojure support. The optional first parameter is a map of options. The following options are available: | Mapper options | | | ------------------- | ---------------------------------------------------------- | | `:modules` | vector of extra ObjectMapper modules | | `:factory` | A Jackson JsonFactory for this given mapper | | `:mapper` | The base ObjectMapper to start with - overrides `:factory` | | Encoding options | | | ------------------- | ------------------------------------------------- | | `:order-by-keys` | set to true to order map keys alphabetically | | `:pretty` | set to true use Jacksons pretty-printing defaults | | `:escape-non-ascii` | set to true to escape non ascii characters | | `:strip-nils` | remove any keys that have nil values | | `:date-format` | string for custom date formatting. default: `yyyy-MM-dd'T'HH:mm:ss'Z'` | | `:encode-key-fn` | true to coerce keyword keys to strings, false to leave them as keywords, or a function to provide custom coercion (default: true) | | `:encoders` | a map of custom encoders where keys should be types and values should be encoder functions | Encoder functions take two parameters: the value to be encoded and a JsonGenerator object. The function should call JsonGenerator methods to emit the desired JSON. | Decoding options | | | ------------------- | -------------------------------------------------------------- | | `:decode-key-fn` | true to coerce keys to keywords, false to leave them as strings, or a function to provide custom coercion (default: false) | | `:bigdecimals` | true to decode doubles as BigDecimals (default: false) |
(read-value object)
(read-value object mapper)
Decodes a value from a JSON from anything that
satisfies ReadValue
protocol. By default,
File, URL, String, Reader and InputStream are supported.
To configure, pass in an ObjectMapper created with object-mapper
,
see object-mapper
docstring for the available options.
Decodes a value from a JSON from anything that satisfies [[ReadValue]] protocol. By default, File, URL, String, Reader and InputStream are supported. To configure, pass in an ObjectMapper created with [[object-mapper]], see [[object-mapper]] docstring for the available options.
(-read-value this mapper)
(write-value to object)
(write-value to object mapper)
Encode a value as JSON and write using the provided WriteValue
instance.
By default, File, OutputStream, DataOutput and Writer are supported.
To configure, pass in an ObjectMapper created with object-mapper
,
see object-mapper
docstring for the available options.
Encode a value as JSON and write using the provided [[WriteValue]] instance. By default, File, OutputStream, DataOutput and Writer are supported. To configure, pass in an ObjectMapper created with [[object-mapper]], see [[object-mapper]] docstring for the available options.
(write-value-as-bytes object)
(write-value-as-bytes object mapper)
Encode a value as a JSON byte-array.
To configure, pass in an ObjectMapper created with object-mapper
.
Encode a value as a JSON byte-array. To configure, pass in an ObjectMapper created with [[object-mapper]].
(write-value-as-string object)
(write-value-as-string object mapper)
Encode a value as a JSON string.
To configure, pass in an ObjectMapper created with object-mapper
.
Encode a value as a JSON string. To configure, pass in an ObjectMapper created with [[object-mapper]].
(-write-value this value mapper)
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close