unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: "Edward O'Connor" <hober0@gmail.com>
To: emacs-devel@gnu.org
Cc: Nic James Ferrier <nferrier@tapsellferrier.co.uk>
Subject: patch: strict key encoding for json.el
Date: Mon, 20 Aug 2012 14:15:08 -0700	[thread overview]
Message-ID: <CAME3nG+m+PT5vwVHjUHbn9ybDOSBAqG=nUimPZafMQEetKMGvw@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 367 bytes --]

Hi,

Nic Ferrier pointed out that the JSON encoder can produce invalid JSON
object keys in some cases. To guard against this, the attached patch
adds a new function, `json-encode-key', which throws if it's object
doesn't map to a JSON string. It then uses this function everywhere it
creates JSON objects. I've only lightly tested it, but "It Should
Work(TM)".


Ted

[-- Attachment #2: json-strict-key-encoding.patch --]
[-- Type: application/octet-stream, Size: 2018 bytes --]

--- orig.el	2012-08-20 14:11:38.000000000 -0700
+++ json.el	2012-08-20 14:11:03.000000000 -0700
@@ -174,6 +174,10 @@
 (put 'json-string-format 'error-conditions
      '(json-string-format json-error error))
 
+(put 'json-key-format 'error-message "Bad JSON object key")
+(put 'json-key-format 'error-conditions
+     '(json-key-format json-error error))
+
 (put 'json-object-format 'error-message "Bad JSON object")
 (put 'json-object-format 'error-conditions
      '(json-object-format json-error error))
@@ -321,6 +325,15 @@
   "Return a JSON representation of STRING."
   (format "\"%s\"" (mapconcat 'json-encode-char string "")))
 
+(defun json-encode-key (object)
+  "Return a JSON representation of OBJECT.
+If the resulting JSON object isn't a valid JSON object key,
+this signals `json-key-format'."
+  (let ((encoded (json-encode object)))
+    (unless (stringp (json-read-from-string encoded))
+      (signal 'json-key-format (list object)))
+    encoded))
+
 ;;; JSON Objects
 
 (defun json-new-object ()
@@ -395,7 +408,7 @@
              (maphash
               (lambda (k v)
                 (push (format "%s:%s"
-                              (json-encode k)
+                              (json-encode-key k)
                               (json-encode v))
                       r))
               hash-table)
@@ -409,7 +422,7 @@
   (format "{%s}"
           (json-join (mapcar (lambda (cons)
                                (format "%s:%s"
-                                       (json-encode (car cons))
+                                       (json-encode-key (car cons))
                                        (json-encode (cdr cons))))
                              alist)
                      ", ")))
@@ -418,7 +431,7 @@
   "Return a JSON representation of PLIST."
   (let (result)
     (while plist
-      (push (concat (json-encode (car plist))
+      (push (concat (json-encode-key (car plist))
                     ":"
                     (json-encode (cadr plist)))
             result)

             reply	other threads:[~2012-08-20 21:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-20 21:15 Edward O'Connor [this message]
2012-08-22  1:29 ` patch: strict key encoding for json.el Glenn Morris

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='CAME3nG+m+PT5vwVHjUHbn9ybDOSBAqG=nUimPZafMQEetKMGvw@mail.gmail.com' \
    --to=hober0@gmail.com \
    --cc=emacs-devel@gnu.org \
    --cc=nferrier@tapsellferrier.co.uk \
    /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).