GNU bug report logs - #32920
Patch: Add variable json-pretty-print-max-indentation-level to allow more flexibility when pretty-printing json

Previous Next

Package: emacs;

Reported by: Jose Arroyo <jose.m.arroyo.se <at> gmail.com>

Date: Wed, 3 Oct 2018 15:21:02 UTC

Severity: wishlist

Full log


Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Jose Arroyo <jose.m.arroyo.se <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: Patch: Add variable json-pretty-print-max-indentation-level to allow
 more flexibility when pretty-printing json
Date: Wed, 3 Oct 2018 17:08:47 +0200
[Message part 1 (text/plain, inline)]
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?
[Message part 2 (text/html, inline)]
[0001-Add-new-variable-json-pretty-print-max-indentation-l.patch (application/octet-stream, attachment)]

This bug report was last modified 5 years and 357 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.