unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#32920: Patch: Add variable json-pretty-print-max-indentation-level to allow more flexibility when pretty-printing json
@ 2018-10-03 15:08 Jose Arroyo
  2019-06-23 21:25 ` Lars Ingebrigtsen
  0 siblings, 1 reply; 2+ messages in thread
From: Jose Arroyo @ 2018-10-03 15:08 UTC (permalink / raw)
  To: 32920


[-- Attachment #1.1: Type: text/plain, Size: 2139 bytes --]

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?

[-- Attachment #1.2: Type: text/html, Size: 3196 bytes --]

[-- Attachment #2: 0001-Add-new-variable-json-pretty-print-max-indentation-l.patch --]
[-- Type: application/octet-stream, Size: 3657 bytes --]

From 5884690a40cfc35c8021139f18228d948011d150 Mon Sep 17 00:00:00 2001
From: Jose M Arroyo <jose.m.arroyo.se@gmail.com>
Date: Wed, 3 Oct 2018 17:03:35 +0200
Subject: [PATCH] Add new variable json-pretty-print-max-indentation-level

This allows more flexibility when pretty-printing json objects with
many nested levels.
---
 lisp/json.el | 30 ++++++++++++++++++++++++------
 1 file changed, 24 insertions(+), 6 deletions(-)

diff --git a/lisp/json.el b/lisp/json.el
index 112f26944b..701fa6b483 100644
--- a/lisp/json.el
+++ b/lisp/json.el
@@ -131,6 +131,11 @@ the current JSON key.")
 `json-read-object' right after reading a JSON array or object,
 respectively.")
 
+(defvar json-pretty-print-max-indentation-level 0
+  "If > 0, defines the maximum number of nested entities that
+will be indented when pretty-printed.
+Used only when `json-encoding-pretty-print' is non-nil")
+
 \f
 
 ;;; Utilities
@@ -178,11 +183,20 @@ Unlike `reverse', this keeps the property-value pairs intact."
     (nreverse res)))
 
 (defmacro json--with-indentation (body)
-  `(let ((json--encoding-current-indentation
-          (if json-encoding-pretty-print
-              (concat json--encoding-current-indentation
-                      json-encoding-default-indentation)
-            "")))
+  `(let* ((json--encoding-current-indentation
+           (if (and
+                json-encoding-pretty-print
+                (/= json-pretty-print-max-indentation-level 0))
+               (concat json--encoding-current-indentation
+                       json-encoding-default-indentation)
+             ""))
+          (json-pretty-print-max-indentation-level
+           (if (and
+                json-encoding-pretty-print
+                (/= json-pretty-print-max-indentation-level 0))
+               (- json-pretty-print-max-indentation-level 1)
+             0
+             )))
      ,body))
 
 ;; Reader utilities
@@ -551,6 +565,7 @@ Please see the documentation of `json-object-type' and `json-key-type'."
                r)
              json-encoding-separator)
             (if (or (not json-encoding-pretty-print)
+                    (= json-pretty-print-max-indentation-level 0)
                     json-encoding-lisp-style-closings)
                 ""
               json--encoding-current-indentation))))
@@ -577,6 +592,7 @@ Please see the documentation of `json-object-type' and `json-key-type'."
                     alist))
            json-encoding-separator)
           (if (or (not json-encoding-pretty-print)
+                  (= json-pretty-print-max-indentation-level 0)
                   json-encoding-lisp-style-closings)
               ""
             json--encoding-current-indentation)))
@@ -600,6 +616,7 @@ Please see the documentation of `json-object-type' and `json-key-type'."
       (concat "{"
               (json-join (nreverse result) json-encoding-separator)
               (if (and json-encoding-pretty-print
+                       (/= json-pretty-print-max-indentation-level 0)
                        (not json-encoding-lisp-style-closings))
                   json--encoding-current-indentation
                 "")
@@ -658,7 +675,8 @@ become JSON objects."
                                     json-encoding-separator
                                     json--encoding-current-indentation))))
        (format "%s]"
-               (if json-encoding-lisp-style-closings
+               (if (or json-encoding-lisp-style-closings
+                       (= json-pretty-print-max-indentation-level 0))
                    ""
                  json--encoding-current-indentation)))
     (concat "["
-- 
2.15.2 (Apple Git-101.1)


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* bug#32920: Patch: Add variable json-pretty-print-max-indentation-level to allow more flexibility when pretty-printing json
  2018-10-03 15:08 bug#32920: Patch: Add variable json-pretty-print-max-indentation-level to allow more flexibility when pretty-printing json Jose Arroyo
@ 2019-06-23 21:25 ` Lars Ingebrigtsen
  0 siblings, 0 replies; 2+ messages in thread
From: Lars Ingebrigtsen @ 2019-06-23 21:25 UTC (permalink / raw)
  To: Jose Arroyo; +Cc: 32920

Jose Arroyo <jose.m.arroyo.se@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





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-06-23 21:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-03 15:08 bug#32920: Patch: Add variable json-pretty-print-max-indentation-level to allow more flexibility when pretty-printing json Jose Arroyo
2019-06-23 21:25 ` Lars Ingebrigtsen

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).