Beautiful print in cljs

I am trying to print JSON from clojurescript in a browser console.

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

In the above link, the following js are JSON.stringify(obj, undefined, 2) - JSON.stringify(obj, undefined, 2)

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

  • Is there any native way in cljs to print?
  • Any ideas why the expression above cljs is not working?
+8
clojure clojurescript
source share
5 answers

UPDATE : ClojureScript now has the full clojure.pprint port as cljs.pprint.

There is also fipp , which already has scope and is probably a bit faster.

+10
source share
 cljs.user> (.stringify js/JSON (clj->js {:foo 42}) nil 2) "{\n \"foo\": 42\n}" cljs.user> (pr-str {:foo 42}) "{:foo 42}" 
+5
source share

The following converts the Clojure ( object ) map to JSON and prints it to the console as an object, which therefore allows browsers to test JSON functionality:

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

EDIT : while the pretty pretty print is really good, in the developer console I still prefer the ability to view the data structure like a tree and now use cljs-devtools often . This is a library that gives you an interactive data tree that can be expanded as a regular js object, but for vanilla Clojure without having to convert to js, ​​which means :keywords , {:ma "ps"} , and the rest of the clj family,

At the moment, this requires you to add leiningen dependencies and some code to your project and use Chrome Canary.

+3
source share

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

+2
source share

In fact, someone needs the clojure.pprint port, which seems to be happening here shaunlebron/cljs-pprint .

At the same time, if you are working in NodeJS, I am using prettyjson from npm.

 (ns foo (:require [cljs.nodejs :as nodejs] [cljs.core :refer [clj->js]])) (nodejs/enable-util-print!) (def render (.-render (nodejs/require "prettyjson"))) (defn pp [value] (println (render (clj->js value)))) 

Then it prints the value in color YAML:

 ClojureScript:foo> (pp {:a 123 :foo ["baz" 42]}) a: 123 foo: - baz - 42 

It's just a hack, but at least it's readable.

+1
source share

All Articles