rudolph-miller / jonathan

JSON encoder and decoder.
http://rudolph-miller.github.io/jonathan/overview.html
164 stars 24 forks source link

Alists encoded incorrectly #32

Closed jorams closed 8 years ago

jorams commented 8 years ago

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"
        }
    }
}
rudolph-miller commented 8 years ago

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\":\"\"}]}"
jorams commented 8 years ago

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.

rudolph-miller commented 8 years ago

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.