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

To reply to this bug, email your comments to 32920 AT debbugs.gnu.org.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#32920; Package emacs. (Wed, 03 Oct 2018 15:21:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Jose Arroyo <jose.m.arroyo.se <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Wed, 03 Oct 2018 15:21:02 GMT) Full text and rfc822 format available.

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)]

Severity set to 'wishlist' from 'normal' Request was from Noam Postavsky <npostavs <at> gmail.com> to control <at> debbugs.gnu.org. (Thu, 25 Oct 2018 00:55:01 GMT) Full text and rfc822 format available.

Added tag(s) patch. Request was from Noam Postavsky <npostavs <at> gmail.com> to control <at> debbugs.gnu.org. (Thu, 25 Oct 2018 00:55:01 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#32920; Package emacs. (Sun, 23 Jun 2019 21:26:02 GMT) Full text and rfc822 format available.

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

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Jose Arroyo <jose.m.arroyo.se <at> gmail.com>
Cc: 32920 <at> debbugs.gnu.org
Subject: Re: bug#32920: Patch: Add variable
 json-pretty-print-max-indentation-level to allow more flexibility when
 pretty-printing json
Date: Sun, 23 Jun 2019 23:25:03 +0200
Jose Arroyo <jose.m.arroyo.se <at> gmail.com> writes:

> 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
>   }
> }

That is, indeed, really bad pretty printing of the arrays.  But I think
the approach you take in the patch isn't ideal, either: If you have
nested hash maps, then you want to have them nicely indented and broken
up into shapes like the one above.

> Whereas my/json-flatten-object-one-level would output:
> {
>   "firstKey": {"46": "0"},
>   "secondKey": {"46": [[[[0,0],0],0],0]},
>   "thirdKey": {"46": 0}
> }

I think a better solution here would be to just handle [] arrays in a
totally different way than the current pretty-printer, and instead keep
them on one line.  Well, at least more on one line than today...

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




Removed tag(s) patch. Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Sun, 23 Jun 2019 21:28:01 GMT) Full text and rfc822 format available.

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

Previous Next


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