rudolph-miller/jonathan

Alists encoded incorrectly

jorams opened this issue · 3 comments

If I take the following JSON and parse it :as :alist:

{
    "test": [{
        "abc": "def",
        "ghi": ""
    }]
}

I get the following alist, which is correct.

(("test" (("ghi" . "") ("abc" . "def"))))

If I then encode that back to JSON :from :alist, what comes out is all wrong.

{
    "test": {
        "(ghi . )": {
            "abc": "def"
        }
    }
}

Thanks for your reporting.

The value of

'(("test" (("ghi" . "") ("abc" . "def"))))

for the key "test" is

'((("ghi" . "") ("abc" . "def")))

, and this can be interpreted as both of

(list (list (cons "ghi" "") (cons "abc" "def")))

and

(list (cons '("ghi" . "") (list (cons "abc" "def"))))

.
It's ambiguous.

If you really want to treat this kind of structure,
please use :as :hash-table .

(jojo:to-json (jojo:parse "{
    \"test\": [{
        \"abc\": \"def\",
        \"ghi\": \"\"
    }]
}" :as :hash-table))
;; => "{\"test\":[{\"abc\":\"def\",\"ghi\":\"\"}]}"

Thanks for your response.

I figured it would be the ambiguity, but I think the interpretation could be better. The only valid keys in a JSON object are strings, so the first interpretation is, in the context of JSON encoding, "more valid" than the second.

I know what you say, but I do not implement the feature, because in this case, it will be invalid that has symbols as its keys.