Zulu-Inuoe / jzon

A correct and safe(er) JSON RFC 8259 reader/writer with sane defaults.
MIT License
151 stars 14 forks source link

Stringify on nested lists #14

Closed sabracrolleton closed 2 years ago

sabracrolleton commented 2 years ago

At times stringify tries too hard to recurse into the data structure. Consider:

(stringify '((foo . bar) (baz . ((1 2 3) (4 5 6)))))
"{\"foo\":\"BAR\",\"baz\":{\"1\":[2,3],\"4\":[5,6]}}"

I was hoping for a result like:

"{\"foo\":\"bar\",\"baz\":[[1,2,3],[4,5,6]]}"

which is what I would get from cl-json. Instead stringify tries to make a key value out of each sublist. Is there any way of getting there?

Zulu-Inuoe commented 2 years ago

Hey @sabracrolleton

When it comes to alist/plist detection, jzon uses heuristics based on if it sees a list with all conses, and one of the following for each car:

This last one is the one causing this. I didn't put too much thought into that and did it out of habit. I doubt there's much use-case out there for integers as object keys, so I don't mind changing the alist/plist detection to not consider integers as keys.

Alternatively, you can ensure something is always encoded as a JSON array by making it a vector rather than a list.

Zulu-Inuoe commented 2 years ago

@sabracrolleton Aside from the integers as keys thing, I noticed you're also expecting symbols to serialize as lowercase in your example. I need to think about this a bit - I did case conversion on object keys (as symbol names) to be pragmatic, but didn't consider what they should serialize as when used as a value.

sabracrolleton commented 2 years ago

Upper case or lower case is not high on my priority list.