I'm trying to do a simple print-json, and am getting a ClassCastException in the data.json library. I'm using [org.clojure/clojure "1.3.0-beta1"] and [org.clojure/data.json "0.1.0"]. So…
> lein repl
…
user => (require 'clojure.data.json)
nil
user => (clojure.data.json/print-json "tim")
ClassCastException java.io.OutputStreamWriter cannot be cast to java.io.PrintWriter clojure.data.json/write-json-string (json.clj:229)
user=> (clojure.data.json/print-json [1 2 3])
ClassCastException java.io.OutputStreamWriter cannot be cast to java.io.PrintWriter clojure.data.json/write-json-array (json.clj:254)
user => (clojure.data.json/print-json { :a { :aa "b" } } )
ClassCastException java.io.OutputStreamWriter cannot be cast to java.io.PrintWriter clojure.data.json/write-json-object (json.clj:238)
Seems fairly straightforward (I've also tried on lists, nested hashes, etc). If I look at the source for json:229 , the 'out' variable looks to be a PrintWriter (and my local source version is the same). And a stacktrace gives exactly that location
user=> (. *e printStackTrace)
java.lang.ClassCastException: java.io.OutputStreamWriter cannot be cast to java.io.PrintWriter
at clojure.data.json$write_json_string.invoke(json.clj:229)
at clojure.data.json$eval108$fn__109$G__99__118.invoke(json.clj:201)
at clojure.data.json$print_json.doInvoke(json.clj:331)
at clojure.lang.RestFn.invoke(RestFn.java:410)
at user$eval212.invoke(NO_SOURCE_FILE:24)
at clojure.lang.Compiler.eval(Compiler.java:6406)
at clojure.lang.Compiler.eval(Compiler.java:6372)
at clojure.core$eval.invoke(core.clj:2745)
at clojure.main$repl$read_eval_print__6016.invoke(main.clj:244)
at clojure.main$repl$fn__6021.invoke(main.clj:265)
at clojure.main$repl.doInvoke(main.clj:265)
at clojure.lang.RestFn.invoke(RestFn.java:512)
at user$eval7$acc__1060__auto____8$fn__10.invoke(NO_SOURCE_FILE:1)
at clojure.lang.AFn.run(AFn.java:24)
at java.lang.Thread.run(Thread.java:636)
nil
user=>
Is there a problem in the data.json lib?
Tim
Ok, so I fixed the problem by changing A) to B)
A)
(defn print-json
...
(write-json x *out* escape-unicode)))
to
B)
(defn print-json
...
(write-json (PrintWriter. *out*) escape-unicode)))
The only thing now, is that the 'nil' return value suffixes itself. I can find out where that is. But I think this this could be fixed easily enough. If you like, I can do this locally and, I guessing, submit a github pull request.
user => (clojure.data.json/print-json "tim")
"tim"nil
user=> (clojure.data.json/print-json [1 2 3])
[1,2,3]nil
user => (clojure.data.json/print-json { :a { :aa "b" } } )
{"a":{"aa":"b"}}nil
Tim
user> (json/json-str {:a "b"})
"{\"a\":\"b\"}"
by the way, the nil in your previous email is not being suffixed to
the string, its simply the return of the function getting written to
stdout by the repl immediately after the function has printed there
also. simply pressing enter at the repl will cause 'nil' to be printed
too...
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your
> first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
--