The Glam package has a tutorial (https://hexdocs.pm/glam/pretty_printing_JSON.html) on how to use it to pretty-print a custom JSON type. Is there an existing pretty-printer for the gleam_json package?
#Pretty printing Json
1 messages · Page 1 of 1 (latest)
There's pprint, which pretty prints all types 🙂
Pretty print values with style!
do you mean the json type cannot be pretty printed at all or with pprint?
Calling pprint.debug with a json.Json doesn't work for me, gives the pictured error.
It'd be great to be able to pretty print the stringified JSON, i.e. with indenting and whitespace similar to what JSON.stringify(json, null, 2) gives you in JS. I opened a GH issue for it a while back and it was closed because with the reason that it would impact the performance of the non pretty printing path. I'm not sure why that is though, I'd assumed they'd be separate paths. Issue link: https://github.com/gleam-lang/json/issues/13.
Maybe JSON pretty printing belongs best as a new feature in the existing pprint package?
GitHub
🐑 Work with JSON in Gleam! Contribute to gleam-lang/json development by creating an account on GitHub.
Hmm, I don't understand the reasoning behind why that wouldn't be supported. At least for JS the to_string is implemented in terms of this: https://github.com/gleam-lang/json/blob/9fecf95c34ff3b76aac89e23f133e121a674266a/src/gleam_json_ffi.mjs#L5 (JSON.stringify), so it would be simple to provide a "pretty" version at least for JS it seems
Yes looks like it would easy for JS (or could DIY easily) - might have been different before the recent v2 changes. Not sure if there's anything for pretty printing in Erlang's json module that was added in v27
I can't see anything related to pretty printing here: https://www.erlang.org/doc/apps/stdlib/json.html#encode/1