Share your thoughts in the 2021 Clojure Community Survey!

Welcome! Please see the About page for a little more info on how this works.

0 votes
in ClojureScript by

Steps

(def foo (let [bar "baz"]
           (reify Object)))
(.stringify js/JSON (clj->js foo))

Or oneliner:

clj -Srepro -Sdeps '{:deps {org.clojure/clojurescript {:mvn/version
"1.10.520"}}}' -m cljs.main -re node -e '(def foo (let [bar "baz"]
(reify Object))) (.stringify js/JSON (clj->js foo))'

Actual

There's bar var value printed which is surprising and may be dangerous if it exposes sensitive data in case of mistake:

"{\"bar\":\"baz\",\"meta529\":{\"meta\":null,\"cnt\":0,\"arr\":[],\"__hash\":-15128758,\"cljs$lang$protocol_mask$partition0$\":16647951,\"cljs$lang$protocol_mask$partition1$\":139268},\"cljs$lang$protocol_mask$partition0$\":393216,\"cljs$lang$protocol_mask$partition1$\":0}"

Expected

No bar in the output.

1 Answer

0 votes
by
selected by
 
Best answer

You need to implement toJSON method
`
(let [bar "baz"]
(reify Object

(toJSON [this]
   (js/JSON.stringify #js{:bar bar})))))

`

This structure printed is just a implementation detail.

...