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

annotations support #28

Open mpenet opened 4 years ago

mpenet commented 4 years ago

I am trying to exclude a field from the serialized output (it causes a cyclic serialization issue -> stack overflow). I cannot (ab)use a custom mapper, it's out of my control in that case (in a dependency).

This seems to be working with deftype but not defrecord:

(deftype D [a])
(deftype T [^{com.fasterxml.jackson.annotation.JsonIgnore true} a])
(defrecord R [^{com.fasterxml.jackson.annotation.JsonIgnore true} a])

;; just so that it doesn't blow up on the deftype
(def mapper
  (doto (object-mapper {})
    (.configure com.fasterxml.jackson.databind.SerializationFeature/FAIL_ON_EMPTY_BEANS false)))

(write-value-as-string (D. 1) mapper) -> {"a": 1}
(write-value-as-string (T. 1) mapper) -> {}
(write-value-as-string (R. 1) mapper) -> {"a": 1}

I would expect it to work the same way for records. I guess this might have to do with the custom serializer for Maps in jsonista.

viesti commented 4 years ago

I guess the record gets interpreted as a java.util.Map and serialized via a MapSerializer which doesn't then interpret annotations since a java.util.Map isn't a POJO.

mpenet commented 4 years ago

Yep