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 [email protected] 416.843.9060 On Tue, Jun 28, 2011 at 11:03 PM, gaz jones <[email protected]>wrote: > 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... > > > On Tue, Jun 28, 2011 at 8:46 PM, Timothy Washington <[email protected]> > wrote: > > 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 > > > > > > > > On Tue, Jun 28, 2011 at 8:59 PM, Timothy Washington <[email protected]> > > wrote: > >> > >> 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 > > > > -- > > You received this message because you are subscribed to the Google > > Groups "Clojure" group. > > To post to this group, send email to [email protected] > > Note that posts from new members are moderated - please be patient with > your > > first post. > > To unsubscribe from this group, send email to > > [email protected] > > For more options, visit this group at > > http://groups.google.com/group/clojure?hl=en > > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to [email protected] > Note that posts from new members are moderated - please be patient with > your first post. > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to [email protected] Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/clojure?hl=en
