bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#24252: 25.1; json.el doesn't distinguish null and empty object


From: Yoichi Nakayama
Subject: bug#24252: 25.1; json.el doesn't distinguish null and empty object
Date: Sat, 20 Aug 2016 08:45:18 +0900

> Why don't you bind json-null to whatever value you need?

(let ((json-null 'NULL)) (json-encode (json-read-from-string "{}")))
"\"nil\""

> By the way, another option to distinguish nil and {} is to bind 
> json-object-type to `hash-table'. An empty hash table is not nil.

It does not work because json-pretty-print overwrites json-object-type.

On Fri, Aug 19, 2016 at 11:06 AM, Dmitry Gutov <dgutov@yandex.ru> wrote:
> Hi!
>
> On 08/17/2016 05:54 PM, Yoichi Nakayama wrote:
>
>> When json-pretty-print applied to "{}", it is
>> unexpectedly converted to "null".
>> This is caused by internal representations of null
>> and empty object are the same:
>>   (json-read-from-string "{}") ; => nil
>>   (json-read-from-string "null") ; => nil
>
>
> Why don't you bind json-null to whatever value you need? Then the results
> will be different:
>
> ELISP> (let ((json-null 'NULL)) (json-read-from-string "null"))
> NULL
>
>> +(defvar json-empty-object :json-empty-object
>> +  "Value to use when reading JSON `{}'.
>> +If this has the same value as `json-null', you might not be able to tell
>> +the difference between `{}' and `null'.  Consider let-binding this
>> +around your call to `json-read' instead of `setq'ing it.")
>
>
> This doesn't look like a good default value, at least. It's not one of the
> types that we parse JSON objects to (alist, plist, hash-table). This will
> break real code.
>
> By the way, another option to distinguish nil and {} is to bind
> json-object-type to `hash-table'. An empty hash table is not nil.



-- 
Yoichi NAKAYAMA





reply via email to

[Prev in Thread] Current Thread [Next in Thread]