[clouchdb-devel] Problems with document-to-json

Peter Eddy peter.eddy at gmail.com
Sat Jun 14 22:12:20 UTC 2008


Hi Christian, thanks for trying out clouchdb.

On Fri, Jun 13, 2008 at 6:14 PM, Christian Haselbach <ch at mr-co.de> wrote:
> I'm just experimenting with clouchdb. As noted in the news section of
> clouchdb, CouchDB's view API changed in 0.8. I wanted to adjust the
> corresponding methods in clouchdb. This worked fine for the ad-hoc view,
> but in the create view I ran into a problem with document-to-json. I
> think it does not handle nested maps correctly, or at least not like
> cl-json.

I've just checked changes into CVS to support the new CouchDb views.
There are examples of their use in examples.lisp and test.lisp, but
not yet in the HTML doc. Here's a quick example based on an example
from the CouchDb views wiki page
http://wiki.apache.org/couchdb/HttpViewApi

(create-ps-view "views"
                (ps-view ("all")
                   (defun map (doc)
                     (if (eql doc.type "customer")
                         (emit null doc))))
                (ps-view ("total_purchases")
                   (defun map (doc)
                     (if (eql doc.type "purchase")
                         (emit doc.customer doc.amount)))
                   (defun reduce (keys values)
                     (return (sum values)))))

The create-ps-view is a simple method that just assumes the view
language is JavaScript.

The ps-view is a macro, and everything after the name ("all" or
"total_purchases") is parenscript. You can evaluate ps-view in the
REPL and see the JavaScript output, which makes for easy debugging.

> For example this create document statement:
> (create-document '((name . "Hi there2")
>                   (content . ((foo . "bar") (bla . "fasel")))
>                   (nested . ((foo . ((bla . ((fasel . "zort"))))))))
>                 :id "hithere2")
> will result in this document:
> {"_id":"hithere2","_rev":"1840089990",
>  "NAME":"Hi there2",
>  "CONTENT":{"FOO":"bar","BLA":"fasel"},
>  "NESTED":[["FOO",["BLA",["FASEL"]]]]}
>
> CONTENT looks fine, but NESTED not so much.

That looks like a bug, see next comment

> Observe the following differencs in document-to-json an
> encode-json-to-string:
> (json:encode-json-to-string '((foo . ((bar . baz)))))
> "{\"foo\":{\"bar\":\"baz\"}}"
> (clouchdb::document-to-json '((foo . ((bar . baz)))))
> "[[\"FOO\",[\"BAR\",]]]"
>
> Is this intendend?

Yes and no. It's intentionally different from the json output, but it
probably shouldn't be losing baz.

document-to-json is intentionally different from the json function
because there were cases where it was impossible to tell the
difference between a map and a list of one element. I'll see if I can
fix this issue, but in the mean time note that the document creation
will work if you use keyword symbols for field names:

(create-document '((:name . "Hi there2")
                            (:content . ((foo . "bar") (bla . "fasel")))
                            (:nested . ((:foo . ((:bla . ((:fasel .
"zort")))))))
                           :id "hithere2")

- Peter



More information about the clouchdb-devel mailing list