all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* patch: strict key encoding for json.el
@ 2012-08-20 21:15 Edward O'Connor
  2012-08-22  1:29 ` Glenn Morris
  0 siblings, 1 reply; 2+ messages in thread
From: Edward O'Connor @ 2012-08-20 21:15 UTC (permalink / raw)
  To: emacs-devel; +Cc: Nic James Ferrier

[-- 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)

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-08-22  1:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-08-20 21:15 patch: strict key encoding for json.el Edward O'Connor
2012-08-22  1:29 ` Glenn Morris

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.