0

In Clojure, I am using 'generate-string' function of cheshire (https://github.com/dakrone/cheshire) library to convert EDN to JSON.

It works fine for EDN without custom readers. For ex:

sample.edn: 
{:foo "bar" :baz {:eggplant [1 2 3]}

sample.clj:
(generate-string (edn/read-string (slurp sample.edn))


Output =>
{
  "foo": "bar",
  "baz": {
    "eggplant": [1,2,3]
  }
}

But if the EDN has custom readers which calls java code to create new object, then it fails to convert EDN output to JSON.

For ex:

EDN file:
{
   "version" "1.0"
   "xyz" #xyz/builder "testString"
}


Clojure code:

(defn getXyz [str]
    (.getXyz (XyzBuilder.) str)
)

(defn custom-readers []
    {'xyz/builder getXyz}
)

(edn/read-string
                {:readers (custom-readers)}
                (slurp filename)
)

If EDN file looks like above, it fails to generate JSON output since EDN output contains hashcode of the object created like

{"version" "1.0", 
 "Xyz" #object[com.java.sample.Xyz 0x6731787b "Xyz [type=type_1]"]}

Is there a way to generate JSON out of above output?

Thanks in advance.

Alan Thompson
  • 29,276
  • 6
  • 41
  • 48
ppp456878
  • 155
  • 8
  • I never used Cheshire myself, but it sounds a lot like you need a custom encoder for your Xyz class. Cheshire documents their usage at https://github.com/dakrone/cheshire#custom-encoders – Stefan Kamphausen Feb 20 '20 at 08:51
  • 1
    Thank you for the suggestion. Adding custom encoder for Xyz class helped. Thanks a lot! – ppp456878 Feb 21 '20 at 01:43

1 Answers1

1

Adding custom encoder like below helps to solve this.

(add-encoder com.java.sample.Xyz
    (fn [c jsonGenerator]
        (.writeString jsonGenerator (str c))))
user51
  • 8,843
  • 21
  • 79
  • 158
ppp456878
  • 155
  • 8