From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.bugs Subject: bug#12634: Patch for pretty-printing in json.el Date: Wed, 14 Nov 2012 20:56:30 -0500 Message-ID: References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1352944657 15276 80.91.229.3 (15 Nov 2012 01:57:37 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 15 Nov 2012 01:57:37 +0000 (UTC) Cc: 12634@debbugs.gnu.org To: Ryan Crum Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Thu Nov 15 02:57:47 2012 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1TYoi6-0004AP-OF for geb-bug-gnu-emacs@m.gmane.org; Thu, 15 Nov 2012 02:57:42 +0100 Original-Received: from localhost ([::1]:49413 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TYohw-0003cX-MI for geb-bug-gnu-emacs@m.gmane.org; Wed, 14 Nov 2012 20:57:32 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:55563) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TYohr-0003cJ-B9 for bug-gnu-emacs@gnu.org; Wed, 14 Nov 2012 20:57:30 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TYoho-00033k-6h for bug-gnu-emacs@gnu.org; Wed, 14 Nov 2012 20:57:27 -0500 Original-Received: from debbugs.gnu.org ([140.186.70.43]:32928) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TYoho-00033g-2e for bug-gnu-emacs@gnu.org; Wed, 14 Nov 2012 20:57:24 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.72) (envelope-from ) id 1TYoiQ-0005B3-2D for bug-gnu-emacs@gnu.org; Wed, 14 Nov 2012 20:58:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Stefan Monnier Original-Sender: debbugs-submit-bounces@debbugs.gnu.org Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 15 Nov 2012 01:58:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 12634 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 12634-submit@debbugs.gnu.org id=B12634.135294463319826 (code B ref 12634); Thu, 15 Nov 2012 01:58:02 +0000 Original-Received: (at 12634) by debbugs.gnu.org; 15 Nov 2012 01:57:13 +0000 Original-Received: from localhost ([127.0.0.1]:43178 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1TYohc-00059h-Bw for submit@debbugs.gnu.org; Wed, 14 Nov 2012 20:57:13 -0500 Original-Received: from ironport2-out.teksavvy.com ([206.248.154.182]:1030) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1TYoha-00059a-8o for 12634@debbugs.gnu.org; Wed, 14 Nov 2012 20:57:10 -0500 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Av0EAG6Zu09sr+ZY/2dsb2JhbABEtBGBCIIVAQEEAVYjBQsLNBIUGA0kiBwFugmQRAOIQppxgViDBw X-IronPort-AV: E=Sophos;i="4.75,637,1330923600"; d="scan'208";a="207643746" Original-Received: from 108-175-230-88.dsl.teksavvy.com (HELO pastel.home) ([108.175.230.88]) by ironport2-out.teksavvy.com with ESMTP/TLS/ADH-AES256-SHA; 14 Nov 2012 20:56:31 -0500 Original-Received: by pastel.home (Postfix, from userid 20848) id 04F11594C8; Wed, 14 Nov 2012 20:56:30 -0500 (EST) In-Reply-To: (Ryan Crum's message of "Tue, 30 Oct 2012 17:04:52 -0400") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux) X-Spam-Score: 0.8 (/) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.13 Precedence: list X-Spam-Score: 0.8 (/) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 140.186.70.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:66940 Archived-At: > OK, let's try this again. > New patch attached. Getting there. But still waiting for the copyright paperwork. Since we still have time, here are some further nitpicks. > @@ -99,6 +100,25 @@ > 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-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 "\n" > + "Internally used to keep track of the current indentation level of > +encoding. Used only when `json-encoding-pretty-print' is non-nil.") Use a "json--" prefix, since it's a convention we use to express that something is internal. > +(defvar json-encoding-pretty-print nil > + "Setting this to non-nil will result in the output of `json-encode' > +to be pretty-printed.") > + > +(defvar json-encoding-lisp-style-closings nil > + "Setting this to `t' will cause ] and } closings to happen lisp-style, > +without indentation.") The first line of a docstring should "stand on it own", i.e. it shouldn't end in the middle of a sentence. Try C-u M-x checkdoc-current-buffer. Rather than "Setting this to will cause ", we usually just say "If , ". E.g. "If non-nil, the output of `json-encode' will be pretty-printed." Also, contrary to all other symbols, t (like nil) is written without `...' quoting. > +(defmacro json--with-indentation (body) > + `(let ((json-encoding-current-indentation > + (if json-encoding-pretty-print > + (concat json-encoding-current-indentation > + json-encoding-default-indentation) > + ""))) > + ,body)) Good. > (defun json-encode-hash-table (hash-table) > "Return a JSON representation of HASH-TABLE." > - (format "{%s}" > + (format (if json-encoding-pretty-print "{%s%s}" "{%s}") Hmm... if json-encoding-pretty-print is nil, we still pass 2 args, and the second is always "", so we can always use "{%s%s}", right? > (json-join > (let (r) > - (maphash > - (lambda (k v) > - (push (format "%s:%s" > - (json-encode-key k) > - (json-encode v)) > - r)) > - hash-table) > + (json--with-indentation > + (maphash > + (lambda (k v) > + (push (format > + (if json-encoding-pretty-print > + "%s%s: %s" > + "%s%s:%s") > + json-encoding-current-indentation > + (json-encode-key k) > + (json-encode v)) > + r)) > + hash-table)) > r) > - ", "))) > + json-encoding-separator) > + (if json-encoding-lisp-style-closings > + "" > + json-encoding-current-indentation))) [ It just occurs to me, that the json printer should print to a buffer, rather than to a string. But let's keep this issue for later. ] > - (concat "[" (mapconcat 'json-encode 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))) Use json--with-indentation here (even if it performs an extra redundant test of json-encoding-pretty-print). Stefan