[cl-json-devel] patch to improve encode-json for lists

Nathan Hawkins utsl at quic.net
Sun Mar 25 00:31:00 UTC 2007


Yes, that's much better. I've been working too hard lately, and just used
the first solution that seemed to work.
Yours is obviously a better approach, which isn't too surprising, I've only
been hacking in Lisp for a couple
of months.

I'm using cl-json to bundle up data from SQL requests into JSON for a
Javascript rpc client, so I really wanted a
way to get lists of objects on the Javascript end.

Thanks,
Nathan

On 3/24/07, Henrik Hjelte <henrik at evahjelte.com> wrote:
>
> On Thu, 2007-03-22 at 08:56 -0400, Nathan Hawkins wrote:
> > This fairly simple patch seems to enable encode-json to handle alists.
> Thanks. I added your first example as a testcase,
> test-encode-json-nathan-hawkins
>
> (defvar *foo* '((a . 1) (b . 2) (c . 3)))
> (encode-json-to-string *foo*)
> --> "{\"a\":1,\"b\":2,\"c\":3}"
>
> When I added your patch several other tests broke. There is a problem
> with you code that you guess the type of of the list from the first
> element. So I instead made a solution that works with your code and
> doesn't break any tests. It is in darcs now.
>
> Also, I have added Pascal Bourguignons patch to darcs, so beware that
> json.asd is now renamed cl-json.asd.
>
> Thanks,
> Henrik
>
> >
> > ;; 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)
> >
> > _______________________________________________
> > cl-json-devel mailing list
> > cl-json-devel at common-lisp.net
> > http://common-lisp.net/cgi-bin/mailman/listinfo/cl-json-devel
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/cl-json-devel/attachments/20070324/cb762ce6/attachment.html>


More information about the cl-json-devel mailing list