Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pretty printing in cljs

I am trying to pretty print a JSON from clojurescript, on the browser's console.

I found the following link - How can I pretty-print JSON using JavaScript?

The above link provides the following js - JSON.stringify(obj, undefined, 2)

The following translation in cljs doesnt work (.stringify js/JSON obj undefined 2)

  1. Is there any native way in cljs for pretty printing?
  2. Any ideas why the above cljs expression doesnt work ?
like image 651
murtaza52 Avatar asked Mar 25 '13 05:03

murtaza52


4 Answers

UPDATE: ClojureScript now has a full port of clojure.pprint in the form of cljs.pprint.

There's also a fipp which is narrower in scope and likely a bit faster.

like image 58
dnolen Avatar answered Sep 22 '22 02:09

dnolen


cljs.user> (.stringify js/JSON (clj->js {:foo 42}) nil 2)
"{\n  \"foo\": 42\n}"

cljs.user> (pr-str {:foo 42})
"{:foo 42}"
like image 29
Dustin Getz Avatar answered Sep 23 '22 02:09

Dustin Getz


The following converts a Clojure map (object) to JSON and prints it in the console as an object that can hence leverage the browsers inspect JSON functionality:

(.dir js/console (clj->js object)) 

EDIT: While pretty printing is really nice, in the developer console I still prefer the ability to browse the data structure as a tree and now frequently use cljs-devtools. It is a library that gives you an interactive data tree that can be expanded as a normal js-object but for vanilla clojure without having to convert to js, meaning :keywords, {:ma "ps"} and the rest of the clj-family.

At the moment it requires you to add a leiningen dependency and some code to your project and to use Chrome Canary.

like image 38
vikeri Avatar answered Sep 19 '22 02:09

vikeri


clojure.pprint has been ported to ClojureScript with release 0.0-3255. It is called cljs.pprint.

like image 41
Leon Grapenthin Avatar answered Sep 20 '22 02:09

Leon Grapenthin