[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#32920: Patch: Add variable json-pretty-print-max-indentation-level t

From: Jose Arroyo
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

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.

(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": [
  "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


PS: Would this kind of patch require me to sign some papers for contributing?

Attachment: 0001-Add-new-variable-json-pretty-print-max-indentation-l.patch
Description: Binary data

reply via email to

[Prev in Thread] Current Thread [Next in Thread]