all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Yoichi Nakayama <yoichi.nakayama@gmail.com>
To: Dmitry Gutov <dgutov@yandex.ru>
Cc: 24252@debbugs.gnu.org
Subject: bug#24252: 25.1; json.el doesn't distinguish null and empty object
Date: Sun, 21 Aug 2016 21:11:16 +0900	[thread overview]
Message-ID: <CAF5D8-sJJCTquCFL13FP7uNAX-5V1x=UseStvAvT3VWXVSKp2g@mail.gmail.com> (raw)
In-Reply-To: <4d97b2a8-5b96-6c22-7b00-95ef8bd5f6fd@yandex.ru>

There was another potential bug that json-encode-key wrongly interpret
symbols internally used in json.el.
The problem is not caused by my previous patch, but the patch
introduces another internal
symbol :json-null and increase possibility to encounter it.

Following patch will fix it and make it safe to apply my second patch

From 4d3ad1d3bbe6b3e47743f42427ac26cf316c12b5 Mon Sep 17 00:00:00 2001
From: Yoichi Nakayama <yoichi.nakayama@gmail.com>
Date: Sun, 21 Aug 2016 20:24:03 +0900
Subject: [PATCH] Make json-encode-key not to signal with valid keys

Despite strings like ":json-false", "t", "nil" are valid object key,
their elisp representation were confused with internal symbols in
json-encode-key and cause json-key-format error unexpectedly.

* (json-encode-key): Rewrite without using json-encode.
---
 lisp/json.el | 32 ++++++++++++++++++++++++--------
 1 file changed, 24 insertions(+), 8 deletions(-)

diff --git a/lisp/json.el b/lisp/json.el
index a439f77..a387b08 100644
--- a/lisp/json.el
+++ b/lisp/json.el
@@ -425,14 +425,30 @@ representation will be parsed correctly."
     (push "\"" res)
     (apply #'concat "\"" (nreverse res))))

-(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))
+(defun json-encode-key (key)
+  "Return a JSON representation of object key.
+If key isn't valid Elisp object as key, this signals `json-key-format'."
+  (let ((json-key-type
+         (if (eq json-key-type nil)
+             (cdr (assq json-object-type '((hash-table . string)
+                                           (alist . symbol)
+                                           (plist . keyword))))
+           json-key-type)))
+    (json-encode-string
+     (cond ((eq json-key-type 'string)
+            (if (stringp key)
+                key
+              (signal 'json-key-format (list key))))
+           ((eq json-key-type 'symbol)
+            (if (symbolp key)
+                (symbol-name key)
+              (signal 'json-key-format (list key))))
+           ((eq json-key-type 'keyword)
+            (if (keywordp key)
+                (substring (symbol-name key) 1)
+              (signal 'json-key-format (list key))))
+           (t
+            (signal 'json-error (list key)))))))

 ;;; JSON Objects

-- 
2.8.1





  reply	other threads:[~2016-08-21 12:11 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-17 14:54 bug#24252: 25.1; json.el doesn't distinguish null and empty object Yoichi Nakayama
2016-08-19  2:06 ` Dmitry Gutov
2016-08-19 23:45   ` Yoichi Nakayama
2016-08-20  0:52     ` Dmitry Gutov
2016-08-20  6:12       ` Yoichi Nakayama
2016-08-21  1:30         ` Yoichi Nakayama
2016-08-21  3:42         ` Dmitry Gutov
2016-08-21 12:11           ` Yoichi Nakayama [this message]
2016-08-21 13:32             ` Yoichi Nakayama
2016-08-21 15:06               ` Yoichi Nakayama
2016-08-27  0:05                 ` Dmitry Gutov
2018-05-17 14:39                 ` Damien Cassou
2018-05-19  6:52 ` Damien Cassou
2018-05-28 15:21   ` Nicolas Petton
2018-06-11 13:36     ` Damien Cassou
2018-06-12 17:14       ` Eli Zaretskii
2018-06-13  7:13         ` Damien Cassou
2018-06-13 13:05           ` Nicolas Petton
2018-06-13 16:55             ` Eli Zaretskii
2018-06-14  9:04               ` Nicolas Petton

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAF5D8-sJJCTquCFL13FP7uNAX-5V1x=UseStvAvT3VWXVSKp2g@mail.gmail.com' \
    --to=yoichi.nakayama@gmail.com \
    --cc=24252@debbugs.gnu.org \
    --cc=dgutov@yandex.ru \
    /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 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.