Zulu-Inuoe/jzon

Stringify on nested lists

sabracrolleton opened this issue · 3 comments

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?

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:

  • character
  • string
  • symbol
  • integer

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.

@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.

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