Hello everyone,
I came across a use-case when pretty-printing nested JSON objects. The current
json-pretty-print function currently only supports unnesting and indenting all
levels of nesting in a JSON object. I came across a case where I only wanted to
unnest a single level.
I added a new variable (json-pretty-print-max-indentation-level) that allows controlling
the number of unnests performed by json-pretty-print.
E.g:
(defun my/json-flatten-object-one-level (begin end)
"(my/json-flatten-object-one-level BEGIN END) Pretty-print selected region but only one level."
(interactive "r")
(let ((json-pretty-print-max-indentation-level 1))
(json-pretty-print begin end)))
For example, if we have the following json
{"firstKey": {"46": "0"},"secondKey": {"46": [[[[0,0],0],0],0]},"thirdKey": {"46": 0}}
The current json-pretty-print outputs:
{
"firstKey": {
"46": "0"
},
"secondKey": {
"46": [
[
[
[
0,
0
],
0
],
0
],
0
]
},
"thirdKey": {
"46": 0
}
}
Whereas my/json-flatten-object-one-level would output:
{
"firstKey": {"46": "0"},
"secondKey": {"46": [[[[0,0],0],0],0]},
"thirdKey": {"46": 0}
}
I've attached the patch for this, it's still missing the NEWS entries and such.
However, I'm a noob in elisp and the current implementation feels kinda wrong.
When json-pretty-print-max-indentation-level is set to 0, we ignore it by
decrementing it continously so we get into the negative numbers and the check in
json--with-indentation "just works" because json-pretty-print-max-indentation-level
is not 0. So given that it is now a negative number, it'll never be 0 again (unless
there is a gigantic json that makes it overflow :p) so any number of nested entities
are pretty-printed.
I imagine this is not good enough but I haven't figured out something better.
Is there a better way to do this? Any pointers?
Thank you
José
PS: Would this kind of patch require me to sign some papers for contributing?