unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Ryan Crum <ryan.j.crum@gmail.com>
To: 12634@debbugs.gnu.org
Subject: bug#12634: Patch for pretty-printing in json.el
Date: Thu, 25 Oct 2012 10:55:39 -0400	[thread overview]
Message-ID: <CAMiu-qD6-eCYvabbj9zsxFmrotu1vRBacozU4v8bUNaESNuB-Q@mail.gmail.com> (raw)
In-Reply-To: <m2fw5ircjm.fsf@gmail.com>


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

Hi,

I've consolidated my changes into json.el. I have not yet received the form
for copyright assignment in the mail, but I have requested it. Please
advise me if you'd like me to make any changes here, and I'll be happy to
accomodate. Patch attached.

Thanks,

-Ryan

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

[-- Attachment #2: pretty-printing-patch --]
[-- Type: application/octet-stream, Size: 10039 bytes --]

*** a/json.el
--- b/json.el
***************
*** 3,9 ****
  ;; Copyright (C) 2006-2012 Free Software Foundation, Inc.
  
  ;; Author: Edward O'Connor <ted@oconnor.cx>
! ;; Version: 1.3
  ;; Keywords: convenience
  
  ;; This file is part of GNU Emacs.
--- 3,9 ----
  ;; Copyright (C) 2006-2012 Free Software Foundation, Inc.
  
  ;; Author: Edward O'Connor <ted@oconnor.cx>
! ;; Version: 1.4
  ;; Keywords: convenience
  
  ;; This file is part of GNU Emacs.
***************
*** 48,53 ****
--- 48,54 ----
  ;; 2006-12-29 - XEmacs support, from Aidan Kehoe <kehoea@parhasard.net>.
  ;; 2008-02-21 - Installed in GNU Emacs.
  ;; 2011-10-17 - Patch `json-alist-p' and `json-plist-p' to avoid recursion -tzz
+ ;; 2012-10-25 - Added pretty-printed reformatting -Ryan Crum (ryan@ryancrum.org)
  
  ;;; Code:
  
***************
*** 99,104 **** If this has the same value as `json-false', you might not be able to
--- 100,123 ----
  tell the difference between `false' and `null'.  Consider let-binding
  this around your call to `json-read' instead of `setq'ing it.")
  
+ (defvar json-encoding-default-separator ", "
+   "Value to use as an element seperator when encoding.")
+ 
+ (defvar json-encoding-current-separator json-encoding-default-separator
+   "Value to use as an element seperator when encoding.")
+ 
+ (defvar json-encoding-default-indentation "  "
+   "The default indentation level for encoding. Used only when
+ `json-encoding-pretty-print' is non-nil.")
+ 
+ (defvar json-encoding-current-indentation ""
+   "Internally used to keep track of the current indentation level of
+ encoding. Used only when `json-encoding-pretty-print' is non-nil.")
+ 
+ (defvar json-encoding-pretty-print nil
+   "Setting this to non-nil will result in the output of `json-encode'
+ to be pretty-printed.")
+ 
  \f
  
  ;;; Utilities
***************
*** 389,429 **** Please see the documentation of `json-object-type' and `json-key-type'."
  
  (defun json-encode-hash-table (hash-table)
    "Return a JSON representation of HASH-TABLE."
!   (format "{%s}"
            (json-join
             (let (r)
!              (maphash
!               (lambda (k v)
!                 (push (format "%s:%s"
!                               (json-encode k)
!                               (json-encode v))
!                       r))
!               hash-table)
               r)
!            ", ")))
  
  ;; List encoding (including alists and plists)
  
  (defun json-encode-alist (alist)
    "Return a JSON representation of ALIST."
!   (format "{%s}"
!           (json-join (mapcar (lambda (cons)
!                                (format "%s:%s"
!                                        (json-encode (car cons))
!                                        (json-encode (cdr cons))))
!                              alist)
!                      ", ")))
  
  (defun json-encode-plist (plist)
    "Return a JSON representation of PLIST."
    (let (result)
      (while plist
!       (push (concat (json-encode (car plist))
!                     ":"
!                     (json-encode (cadr plist)))
!             result)
        (setq plist (cddr plist)))
!     (concat "{" (json-join (nreverse result) ", ") "}")))
  
  (defun json-encode-list (list)
    "Return a JSON representation of LIST.
--- 408,479 ----
  
  (defun json-encode-hash-table (hash-table)
    "Return a JSON representation of HASH-TABLE."
!   (format (if json-encoding-pretty-print "{\n%s\n%s}" "{%s}")
            (json-join
             (let (r)
!              (let ((json-encoding-current-indentation
!                     (if json-encoding-pretty-print
!                         (concat json-encoding-current-indentation
!                                 json-encoding-default-indentation)
!                       "")))
!                (maphash
!                 (lambda (k v)
!                   (push (format (if json-encoding-pretty-print "%s%s: %s" "%s%s:%s")
!                                 json-encoding-current-indentation
!                                 (json-encode k)
!                                 (json-encode v))
!                         r))
!                 hash-table))
               r)
!            json-encoding-current-separator)
!           json-encoding-current-indentation))
  
  ;; List encoding (including alists and plists)
  
  (defun json-encode-alist (alist)
    "Return a JSON representation of ALIST."
!   (format (if json-encoding-pretty-print "{\n%s\n%s}" "{%s}")
!           (json-join
!            (let ((json-encoding-current-indentation
!                   (if json-encoding-pretty-print
!                       (concat json-encoding-current-indentation
!                               json-encoding-default-indentation)
!                     "")))
!              (mapcar (lambda (cons)
!                        (format (if json-encoding-pretty-print
!                                    "%s%s: %s"
!                                  "%s%s:%s")
!                                json-encoding-current-indentation
!                                (json-encode (car cons))
!                                (json-encode (cdr cons))))
!                      alist))
!                      json-encoding-current-separator)
!           json-encoding-current-indentation))
  
  (defun json-encode-plist (plist)
    "Return a JSON representation of PLIST."
    (let (result)
      (while plist
!       (let ((json-encoding-current-indentation
!              (if json-encoding-pretty-print
!                  (concat json-encoding-current-indentation
!                          json-encoding-default-indentation)
!                "")))
!         (push (concat
!                json-encoding-current-indentation
!                (json-encode (car plist))
!                (if json-encoding-current-indentation
!                    ": "
!                  ":")
!                (json-encode (cadr plist)))
!               result))
        (setq plist (cddr plist)))
!     (concat (if json-encoding-pretty-print "{\n" "{")
!             (json-join (nreverse result) json-encoding-current-separator)
!             (if json-encoding-pretty-print
!                 (format "\n%s" json-encoding-current-indentation)
!               "")
!             "}")))
  
  (defun json-encode-list (list)
    "Return a JSON representation of LIST.
***************
*** 462,468 **** become JSON objects."
  
  (defun json-encode-array (array)
    "Return a JSON representation of ARRAY."
!   (concat "[" (mapconcat 'json-encode array ", ") "]"))
  
  \f
  
--- 512,530 ----
  
  (defun json-encode-array (array)
    "Return a JSON representation of ARRAY."
!   (if (and json-encoding-pretty-print
!            (> (length array) 0))
!       (concat
!        (let ((json-encoding-current-indentation
!               (concat json-encoding-current-indentation
!                       json-encoding-default-indentation)))
!          (concat (format "[\n%s" json-encoding-current-indentation)
!                  (json-join (mapcar 'json-encode array)
!                             (format "%s%s"
!                                     json-encoding-current-separator
!                                     json-encoding-current-indentation))))
!        (format "\n%s]" json-encoding-current-indentation))
!     (concat "[" (mapconcat 'json-encode array json-encoding-current-separator) "]")))
  
  \f
  
***************
*** 516,533 **** Advances point just past JSON object."
  
  (defun json-encode (object)
    "Return a JSON representation of OBJECT as a string."
!   (cond ((memq object (list t json-null json-false))
!          (json-encode-keyword object))
!         ((stringp object)      (json-encode-string object))
!         ((keywordp object)     (json-encode-string
!                                 (substring (symbol-name object) 1)))
!         ((symbolp object)      (json-encode-string
!                                 (symbol-name object)))
!         ((numberp object)      (json-encode-number object))
!         ((arrayp object)       (json-encode-array object))
!         ((hash-table-p object) (json-encode-hash-table object))
!         ((listp object)        (json-encode-list object))
!         (t                     (signal 'json-error (list object)))))
  
  (provide 'json)
  
--- 578,622 ----
  
  (defun json-encode (object)
    "Return a JSON representation of OBJECT as a string."
!   (let ((json-encoding-current-separator
!          (if json-encoding-pretty-print
!              (concat json-encoding-default-separator "\n")
!            json-encoding-default-separator)))
!     (cond ((memq object (list t json-null json-false))
!            (json-encode-keyword object))
!           ((stringp object)      (json-encode-string object))
!           ((keywordp object)     (json-encode-string
!                                   (substring (symbol-name object) 1)))
!           ((symbolp object)      (json-encode-string
!                                   (symbol-name object)))
!           ((numberp object)      (json-encode-number object))
!           ((arrayp object)       (json-encode-array object))
!           ((hash-table-p object) (json-encode-hash-table object))
!           ((listp object)        (json-encode-list object))
!           (t                     (signal 'json-error (list object))))))
! 
! ;; Pretty printing
! 
! (defun json-pretty-print-buffer ()
!   "Pretty-print current buffer."
!   (interactive)
!   (let* ((json-encoding-pretty-print t)
!          (json-string (json-encode (json-read-from-string (buffer-string))))
!          (buf (current-buffer)))
!     (with-current-buffer buf
!       (erase-buffer)
!       (insert json-string))))
! 
! (defun json-pretty-print ()
!   "Pretty-print selected region."
!   (interactive)
!   (unless mark-active
!     (error "No region selected."))
!   (let ((begin (region-beginning))
!         (end (region-end))
!         (json-encoding-pretty-print t))
!     (kill-region begin end)
!     (insert (json-encode (json-read-from-string (current-kill 0))))))
  
  (provide 'json)
  

  parent reply	other threads:[~2012-10-25 14:55 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-13  8:39 bug#12634: 24.2; [FEATURE REQUEST] JSON pretty printer Leo
2012-10-13 21:56 ` Stefan Monnier
2012-10-18 21:18 ` bug#12634: existing implementation Aurélien Aptel
2012-10-19  1:37   ` Stefan Monnier
2012-10-19 16:20     ` Aurélien Aptel
2012-10-19 18:04       ` Stefan Monnier
2012-10-19 21:28         ` Stefan Monnier
2012-10-25 14:55 ` Ryan Crum [this message]
2012-10-25 18:08   ` bug#12634: Patch for pretty-printing in json.el Stefan Monnier
2012-10-27 19:31     ` Ryan Crum
2012-10-30 20:03       ` Stefan Monnier
2012-10-30 21:04         ` Ryan Crum
2012-11-15  1:56           ` Stefan Monnier
2012-11-17 17:37             ` Ryan Crum
2012-11-20 18:52               ` Stefan Monnier
2012-11-27 23:15                 ` Ryan Crum
2012-12-14  2:56 ` bug#12634: Patch Ryan Crum
2012-12-14 14:59   ` Stefan Monnier

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

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAMiu-qD6-eCYvabbj9zsxFmrotu1vRBacozU4v8bUNaESNuB-Q@mail.gmail.com \
    --to=ryan.j.crum@gmail.com \
    --cc=12634@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 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).