jsony icon indicating copy to clipboard operation
jsony copied to clipboard

Feature suggestion: toJson with optional `pretty` bool parameter

Open daehee opened this issue 4 years ago • 3 comments

toJson proc accepts optional pretty bool parameter to pretty-print output with newlines and indents.

daehee avatar Feb 01 '21 19:02 daehee

One one hand adding pretty to the json lib might make it slower and harder to code for as well as how should user hooks work, they would have to worry about formatting as well...

But then maybe I should make pretty be a separate step from turning stuff into json. Maybe pretty should be self contained where it reads and outputs json. I could see some thing like this.

treeform avatar Feb 01 '21 22:02 treeform

Maybe some thing like this?

proc toJsonPretty*[T](v: T): string =
  ## Pretty print json (does not aim to be fast).
  let nonPrettyStr = v.toJson()
  parseJson(nonPrettyStr).pretty()

treeform avatar Feb 01 '21 22:02 treeform

Thanks for looking at this. That makes sense on separating the concerns.

My hack at the moment is actually similar e.g. obj.toJson().fromJson().pretty().

As context, my current use case is scraping/monitoring a data set and outputting "pretty-printed" json files into a git repo, so any optimizations on speed would be helpful.

daehee avatar Feb 01 '21 22:02 daehee