metosin / jsonista

Clojure library for fast JSON encoding and decoding.
https://cljdoc.org/d/metosin/jsonista
Eclipse Public License 2.0
422 stars 30 forks source link

ClassCastException when passing a map of options to `read-value` or `write-value` #13

Closed lambdahands closed 6 years ago

lambdahands commented 6 years ago

Hi there,

I was trying out jsonista, and I noticed the docstrings for read-value and write-value state:

To configure, pass in an ObjectMapper created with [[object-mapper]], or pass in a map with options.

I'm able to pass in an ObjectMapper with no issues:

(require '[jsonista.core :as j])
(j/read-value "{\"hello\": 1}" (j/object-mapper {:decode-key-fn true}))
;; => {:hello 1}

(require '[clojure.string :as str])
(j/write-value-as-string {:hello 1} (j/object-mapper {:encode-key-fn (comp str/upper-case name)}))
;; => "{\"HELLO\":1}"

However, if I only pass a map I get an error:

(j/read-value "{\"hello\": 1}" {:decode-key-fn true})
;; ClassCastException clojure.lang.PersistentArrayMap cannot be cast to com.fasterxml.jackson.databind.ObjectMapper  jsonista.core/eval221/fn--222 (core.clj:142)

(j/write-value-as-string {:hello 1} {:encode-key-fn (comp str/upper-case name)})
;; ClassCastException clojure.lang.PersistentArrayMap cannot be cast to com.fasterxml.jackson.databind.ObjectMapper  jsonista.core/write-value-as-string (core.clj:203)
ikitommi commented 6 years ago

Good catch! The docs were out of sync and fixed now.

We removed the automatic option map coercion into object-mapper both to make the functions bit faster and to make the cost of configuration visible. Creation of object-mapper has a cost and it should be created once and reused in encoding & decoding.

lambdahands commented 6 years ago

Awesome, thanks for the quick response! 😄