diff options
| author | Edward O'Connor | 2012-08-21 21:29:22 -0400 |
|---|---|---|
| committer | Glenn Morris | 2012-08-21 21:29:22 -0400 |
| commit | 94e0e5592ad531a577f4249564da8688d9d9ebea (patch) | |
| tree | 25bebd2bfa1cdacd0f9b83ce33d4fb8d4559a889 | |
| parent | da485f5e6b5fd38d202a0f6682f6fec28963cf1e (diff) | |
| download | emacs-94e0e5592ad531a577f4249564da8688d9d9ebea.tar.gz emacs-94e0e5592ad531a577f4249564da8688d9d9ebea.zip | |
strict key encoding for json.el
Ref: http://lists.gnu.org/archive/html/emacs-devel/2012-08/msg00642.html
* lisp/json.el (json-key-format): Add error properties.
(json-encode-key): New function.
(json-encode-hash-table, json-encode-alist, json-encode-plist):
Use json-encode-key.
| -rw-r--r-- | lisp/ChangeLog | 7 | ||||
| -rw-r--r-- | lisp/json.el | 19 |
2 files changed, 23 insertions, 3 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 748fab9dbe0..4f8de2291db 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog | |||
| @@ -1,3 +1,10 @@ | |||
| 1 | 2012-08-22 Edward O'Connor <hober0@gmail.com> | ||
| 2 | |||
| 3 | * json.el (json-key-format): Add error properties. | ||
| 4 | (json-encode-key): New function. | ||
| 5 | (json-encode-hash-table, json-encode-alist, json-encode-plist): | ||
| 6 | Use json-encode-key. | ||
| 7 | |||
| 1 | 2012-08-22 Glenn Morris <rgm@gnu.org> | 8 | 2012-08-22 Glenn Morris <rgm@gnu.org> |
| 2 | 9 | ||
| 3 | * calendar/cal-tex.el (cal-tex-longday): New function, replacing... | 10 | * calendar/cal-tex.el (cal-tex-longday): New function, replacing... |
diff --git a/lisp/json.el b/lisp/json.el index 468358ccd1a..f1ee3a52032 100644 --- a/lisp/json.el +++ b/lisp/json.el | |||
| @@ -174,6 +174,10 @@ this around your call to `json-read' instead of `setq'ing it.") | |||
| 174 | (put 'json-string-format 'error-conditions | 174 | (put 'json-string-format 'error-conditions |
| 175 | '(json-string-format json-error error)) | 175 | '(json-string-format json-error error)) |
| 176 | 176 | ||
| 177 | (put 'json-key-format 'error-message "Bad JSON object key") | ||
| 178 | (put 'json-key-format 'error-conditions | ||
| 179 | '(json-key-format json-error error)) | ||
| 180 | |||
| 177 | (put 'json-object-format 'error-message "Bad JSON object") | 181 | (put 'json-object-format 'error-message "Bad JSON object") |
| 178 | (put 'json-object-format 'error-conditions | 182 | (put 'json-object-format 'error-conditions |
| 179 | '(json-object-format json-error error)) | 183 | '(json-object-format json-error error)) |
| @@ -321,6 +325,15 @@ representation will be parsed correctly." | |||
| 321 | "Return a JSON representation of STRING." | 325 | "Return a JSON representation of STRING." |
| 322 | (format "\"%s\"" (mapconcat 'json-encode-char string ""))) | 326 | (format "\"%s\"" (mapconcat 'json-encode-char string ""))) |
| 323 | 327 | ||
| 328 | (defun json-encode-key (object) | ||
| 329 | "Return a JSON representation of OBJECT. | ||
| 330 | If the resulting JSON object isn't a valid JSON object key, | ||
| 331 | this signals `json-key-format'." | ||
| 332 | (let ((encoded (json-encode object))) | ||
| 333 | (unless (stringp (json-read-from-string encoded)) | ||
| 334 | (signal 'json-key-format (list object))) | ||
| 335 | encoded)) | ||
| 336 | |||
| 324 | ;;; JSON Objects | 337 | ;;; JSON Objects |
| 325 | 338 | ||
| 326 | (defun json-new-object () | 339 | (defun json-new-object () |
| @@ -395,7 +408,7 @@ Please see the documentation of `json-object-type' and `json-key-type'." | |||
| 395 | (maphash | 408 | (maphash |
| 396 | (lambda (k v) | 409 | (lambda (k v) |
| 397 | (push (format "%s:%s" | 410 | (push (format "%s:%s" |
| 398 | (json-encode k) | 411 | (json-encode-key k) |
| 399 | (json-encode v)) | 412 | (json-encode v)) |
| 400 | r)) | 413 | r)) |
| 401 | hash-table) | 414 | hash-table) |
| @@ -409,7 +422,7 @@ Please see the documentation of `json-object-type' and `json-key-type'." | |||
| 409 | (format "{%s}" | 422 | (format "{%s}" |
| 410 | (json-join (mapcar (lambda (cons) | 423 | (json-join (mapcar (lambda (cons) |
| 411 | (format "%s:%s" | 424 | (format "%s:%s" |
| 412 | (json-encode (car cons)) | 425 | (json-encode-key (car cons)) |
| 413 | (json-encode (cdr cons)))) | 426 | (json-encode (cdr cons)))) |
| 414 | alist) | 427 | alist) |
| 415 | ", "))) | 428 | ", "))) |
| @@ -418,7 +431,7 @@ Please see the documentation of `json-object-type' and `json-key-type'." | |||
| 418 | "Return a JSON representation of PLIST." | 431 | "Return a JSON representation of PLIST." |
| 419 | (let (result) | 432 | (let (result) |
| 420 | (while plist | 433 | (while plist |
| 421 | (push (concat (json-encode (car plist)) | 434 | (push (concat (json-encode-key (car plist)) |
| 422 | ":" | 435 | ":" |
| 423 | (json-encode (cadr plist))) | 436 | (json-encode (cadr plist))) |
| 424 | result) | 437 | result) |