This fairly simple patch seems to enable encode-json to handle alists.
After applying:
;; handles alists (defvar *foo* '((a . 1) (b . 2) (c . 3))) (encode-json-to-string *foo*) --> "{"a":1,"b":2,"c":3}"
;; normal lists still work (defvar *bar* '(1 2 3 4 5)) (encode-json-to-string *bar*) --> "[1,2,3,4,5]"
;; messed up lists of alists which contain lists also works now (defvar *baz* '(((a . 1) (b .2) (c . 3) (d . (1 2 3 4 5))))) (encode-json-to-string *baz*) --> "[{"a":1,"b":[0.2],"c":3,"d":[1,2,3,4,5]}]"
;; even better, decode-json likes the output (decode-json-from-string "[{"a":1,"b":[0.2 ],"c":3,"d":[1,2,3,4,5]}]") --> (((:A . 1) (:B 0.2) (:C . 3) (:D 1 2 3 4 5)))
One thing you can't do is encode a list appended to an alist. That won't work, and I don't know how you'd encode it in JSON anyway.
Someone who's had way more sleep than I have lately should test this as well. But I can now both encode and decode lists of alists, which scratches my particular itch...
Nathan
diff -rN -u old-cl-json/src/encoder.lisp new-cl-json/src/encoder.lisp --- old-cl-json/src/encoder.lisp 2007-03-21 23:29:22.000000000 -0400 +++ new-cl-json/src/encoder.lisp 2007-03-21 23:29:22.000000000 -0400 @@ -25,6 +25,12 @@ (t (write-json-string (funcall *symbol-to-string-fn* s) stream))))
(defmethod encode-json((s sequence ) stream) + (if (and (consp (car s)) + (atom (cdar s))) + (encode-json-alist s stream) + (encode-json-list s stream))) + +(defun encode-json-list (s stream) (let ((first-element t)) (write-char #[ stream) (map nil #'(lambda (element)