unofficial mirror of emacs-devel@gnu.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

* Re: patch: strict key encoding for json.el
  2012-08-20 21:15 patch: strict key encoding for json.el Edward O'Connor
@ 2012-08-22  1:29 ` Glenn Morris
  0 siblings, 0 replies; 2+ messages in thread
From: Glenn Morris @ 2012-08-22  1:29 UTC (permalink / raw)
  To: Edward O'Connor; +Cc: Nic James Ferrier, emacs-devel


Mindlessly applied to trunk since you are the original author.



^ 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 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).