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/from-json +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/from-json +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.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close