From: Jose Arroyo <jose.m.arroyo.se@gmail.com>
To: 32920@debbugs.gnu.org
Subject: bug#32920: 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 [thread overview]
Message-ID: <CANg9NwXwpbaBoVh_sQf7-QG87e3SUGBhBbt0Q4ux4CGABjcEHA@mail.gmail.com> (raw)
[-- 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)
next reply other threads:[~2018-10-03 15:08 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-10-03 15:08 Jose Arroyo [this message]
2019-06-23 21:25 ` bug#32920: Patch: Add variable json-pretty-print-max-indentation-level to allow more flexibility when pretty-printing json Lars Ingebrigtsen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CANg9NwXwpbaBoVh_sQf7-QG87e3SUGBhBbt0Q4ux4CGABjcEHA@mail.gmail.com \
--to=jose.m.arroyo.se@gmail.com \
--cc=32920@debbugs.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.