cheshire - Encoding records as JSON objects with additional type field in Clojure -
cheshire's custom encoders seem suitable problem , wrote little helper function:
(defn add-rec-encoder [rec type-token] (add-encoder rec (fn [rec jg] (.writestring jg (str (encode-map (assoc rec :type type-token) jg)))))) (defrecord [a]) (add-rec-encoder "a") (encode (->a "abc"))
but produces strange trailing ""
.
=> {"a":"abc","type":"a"} ""
what causing this? , there approach worth considering (i need able decode record based on type-token)?
(encode-map ... jg)
directly writes encoded map json generator jg
, returns nil
.
this means that, call writestring
actually:
(.writestring jg (str nil))
which, since (str nil)
""
, encode , append json generator. correct encoder logic be:
(defn add-rec-encoder [rec type-token] (add-encoder rec (fn [rec jg] (encode-map (assoc rec :type type-token) jg))))
Comments
Post a Comment