ClassCastException on 'clojure.data.json/print-json'

68 views
Skip to first unread message

Timothy Washington

unread,
Jun 28, 2011, 8:59:10 PM6/28/11
to clo...@googlegroups.com

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 

Timothy Washington

unread,
Jun 28, 2011, 9:46:26 PM6/28/11
to clo...@googlegroups.com

Ok, so I fixed the problem by changing A) to B) 


A) 

(defn print-json

...

    (write-json *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 

gaz jones

unread,
Jun 28, 2011, 11:03:33 PM6/28/11
to clo...@googlegroups.com
are you trying to turn something into a json string? if so, the
json-str function is probably what you are looking for:

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

Timothy Washington

unread,
Jun 28, 2011, 11:54:15 PM6/28/11
to clo...@googlegroups.com
Hey Gareth, 

Yes, in my library, I'm returning a string. So indeed I will be using json-str. But on the command-line, I sometimes want to print or pretty-print something out. I forked data.json and submitted a pull request with the '(PrintWriter. *out*)' fix put in. Hopefully it solves the problem. 

And yes, the repl does spit out the result of the last command. I probably misstated that as a problem (wanted something prettier at the moment). It's not, as json.clj internally uses .print. 


Thanks 

Tim Washington 

Stuart Sierra

unread,
Jun 30, 2011, 8:52:37 AM6/30/11
to clo...@googlegroups.com
clojure.core/*out* is usually a PrintWriter, but its docstring only specifies that it's a Writer. So it's a bug for data.json to assume PrintWriter.  I'll fix it.

-Stuart Sierra
clojure.com

Timothy Washington

unread,
Jun 30, 2011, 10:09:39 AM6/30/11
to clo...@googlegroups.com
I already made a quick fix and put in a github pull request. You can do whichever is easier. 


Tim Washington 



--

Aaron Bedra

unread,
Jun 30, 2011, 3:33:56 PM6/30/11
to clo...@googlegroups.com
The process for getting code into Clojure's official libraries is the same as it is for the language.  We work with patches submitted to JIRA (http://dev.clojure.org/jira) rather than github pull requests.  It allows us to track things a little bit easier.  We also require that all contributors sign the Clojure contributor agreement (referred to as the CA).  I did a quick check and didn't see your name on the list.  Please forgive me if I didn't search properly and you are already on there. 

For this issue in particular, it will probably be easiest to just let Stuart fix things up quickly.  If you are interested in contributing in the future I encourage you to get your CA in and sign up for an account on JIRA.  Make sure not to send your CA certified or require a signature as it will most likely bounce back to you. 

Cheers,

Aaron Bedra
--
Clojure/core
http://clojure.com

Timothy Washington

unread,
Jun 30, 2011, 7:57:03 PM6/30/11
to clo...@googlegroups.com
Ah, I see I see. I think this was a one off thing, and pretty simple - so Stuart fixing it is fine. If I come across a few more patches I'd like to submit, I'll definitely go through this process. Thanks for the information Aaron. 


Tim Washington 
Reply all
Reply to author
Forward
0 new messages