Fork me on GitHub
#malli
<
2022-02-26
>
sound2gd00:02:09

hi clojurians~ I'm stucking on decoding json from string with malli

sound2gd00:02:14

(require '[malli.core :as m])
  (require '[malli.transform :as mt])

  (m/decode [:map
             [:x :string]
             [:y :int]]
            "{\"x\": \"aa\",\"y\": 3}"
            (mt/transformer
             mt/json-transformer))
;; => "{\"x\": \"aa\",\"y\": 3}"

Ben Sless06:02:51

Json transformers assume the data is already deserialized

Ben Sless06:02:09

You need a library like jsonista for that

sound2gd14:02:41

thanks. finally work out like this

sound2gd14:02:47

(m/decode [:string {:decode/string #(json/decode % true)
                      :encode/string json/encode}]
            "{\"x\": \"aa\",\"y\": 3}"
            (mt/transformer
             mt/strip-extra-keys-transformer
             mt/default-value-transformer
             mt/string-transformer))