Yes, that's much better. I've been working too hard lately, and just used the first solution that seemed to work.<br>Yours is obviously a better approach, which isn't too surprising, I've only been hacking in Lisp for a couple
<br>of months.<br><br>I'm using cl-json to bundle up data from SQL requests into JSON for a Javascript rpc client, so I really wanted a<br>way to get lists of objects on the Javascript end.<br><br>Thanks,<br>Nathan<br>
<br><div><span class="gmail_quote">On 3/24/07, <b class="gmail_sendername">Henrik Hjelte</b> <<a href="mailto:henrik@evahjelte.com">henrik@evahjelte.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On Thu, 2007-03-22 at 08:56 -0400, Nathan Hawkins wrote:<br>> This fairly simple patch seems to enable encode-json to handle alists.<br>Thanks. I added your first example as a testcase,<br>test-encode-json-nathan-hawkins
<br><br>(defvar *foo* '((a . 1) (b . 2) (c . 3)))<br>(encode-json-to-string *foo*)<br>--> "{\"a\":1,\"b\":2,\"c\":3}"<br><br>When I added your patch several other tests broke. There is a problem
<br>with you code that you guess the type of of the list from the first<br>element. So I instead made a solution that works with your code and<br>doesn't break any tests. It is in darcs now.<br><br>Also, I have added Pascal Bourguignons patch to darcs, so beware that
<br>json.asd is now renamed cl-json.asd.<br><br>Thanks,<br>Henrik<br><br>><br>> ;; normal lists still work<br>> (defvar *bar* '(1 2 3 4 5))<br>> (encode-json-to-string *bar*)<br>> --> "[1,2,3,4,5]"
<br>><br>> ;; messed up lists of alists which contain lists also works now<br>> (defvar *baz* '(((a . 1) (b .2) (c . 3) (d . (1 2 3 4 5)))))<br>> (encode-json-to-string *baz*)<br>> --> "[{\"a\":1,\"b\":[
0.2],\"c\":3,\"d\":[1,2,3,4,5]}]"<br>><br>> ;; even better, decode-json likes the output<br>> (decode-json-from-string "[{\"a\":1,\"b\":[ 0.2],\"c\":3,\"d
<br>> \":[1,2,3,4,5]}]")<br>> --> (((:A . 1) (:B 0.2) (:C . 3) (:D 1 2 3 4 5)))<br>><br>> One thing you can't do is encode a list appended to an alist. That<br>> won't work, and I don't know how you'd
<br>> encode it in JSON anyway.<br>><br>> Someone who's had way more sleep than I have lately should test this<br>> as well. But I can now both encode<br>> and decode lists of alists, which scratches my particular itch...
<br>><br>> Nathan<br>><br>> diff -rN -u old-cl-json/src/encoder.lisp new-cl-json/src/encoder.lisp<br>> --- old-cl-json/src/encoder.lisp 2007-03-21 23:29:22.000000000<br>> -0400<br>> +++ new-cl-json/src/encoder.lisp 2007-03-21 23:29:
22.000000000<br>> -0400<br>> @@ -25,6 +25,12 @@<br>> (t (write-json-string (funcall *symbol-to-string-fn* s)<br>> stream))))<br>><br>> (defmethod encode-json((s sequence ) stream)<br>> + (if (and (consp (car s))
<br>> + (atom (cdar s)))<br>> + (encode-json-alist s stream)<br>> + (encode-json-list s stream)))<br>> +<br>> +(defun encode-json-list (s stream)<br>> (let ((first-element t))<br>> (write-char #\[ stream)
<br>> (map nil #'(lambda (element)<br>><br>> _______________________________________________<br>> cl-json-devel mailing list<br>> <a href="mailto:cl-json-devel@common-lisp.net">cl-json-devel@common-lisp.net
</a><br>> <a href="http://common-lisp.net/cgi-bin/mailman/listinfo/cl-json-devel">http://common-lisp.net/cgi-bin/mailman/listinfo/cl-json-devel</a><br><br></blockquote></div><br>