dakrone / cheshire

Clojure JSON and JSON SMILE (binary json format) encoding/decoding
https://github.com/dakrone/cheshire
MIT License
1.49k stars 151 forks source link

Pretty-Printing Feature Request: support indenting nested arrays differently than the nesting array #117

Open Josh-Tilles opened 7 years ago

Josh-Tilles commented 7 years ago

The specific use-case I have in mind is printing stack traces.

For example, here's some Clojure code followed by the JSON it produces.

(println
 (cheshire.core/generate-string (Throwable->map (ex-info "foo" {}))
                                {:pretty (cheshire.core/create-pretty-printer
                                          (assoc cheshire/default-pretty-print-options
                                                 :indent-arrays? true))}))
{
  "cause" : "foo",
  "via" : [
    {
      "type" : "clojure.lang.ExceptionInfo",
      "message" : "foo",
      "data" : { },
      "at" : [
        "clojure.core$ex_info",
        "invokeStatic",
        "core.clj",
        4725
      ]
    }
  ],
  "trace" : [
    [
      "clojure.core$ex_info",
      "invokeStatic",
      "core.clj",
      4725
    ],
    [
      "clojure.core$ex_info",
      "invoke",
      "core.clj",
      4725
    ],
    [
      "boot.user$eval2476",
      "invokeStatic",
      "boot.user8810957904944978013.clj",
      1
    ],
    "(omitting the rest)"
  ],
  "data" : { }
}

It would be nice if Cheshire had a convenient way to instead print the data like this:

{
  "cause" : "foo",
  "via" : [
    {
      "type" : "clojure.lang.ExceptionInfo",
      "message" : "foo",
      "data" : { },
      "at" : ["clojure.core$ex_info", "invokeStatic", "core.clj", 4725]
    }
  ],
  "trace" : [
    ["clojure.core$ex_info", "invokeStatic", "core.clj", 4725],
    ["clojure.core$ex_info", "invoke", "core.clj", 4725],
    ["boot.user$eval2480", "invokeStatic", "boot.user8810957904944978013.clj", 1],
    "(omitting the rest)"
  ],
  "data" : { }
}
dakrone commented 7 years ago

@MerelyAPseudonym you should be able to create your own instance of a class extending DefaultPrettyPrinter and plug that in for :pretty, that will let you indent however you would like