From lispercat at gmail.com Mon Jun 9 22:46:31 2008 From: lispercat at gmail.com (Andrei Stebakov) Date: Mon, 9 Jun 2008 18:46:31 -0400 Subject: [cl-json-devel] Parsing JavaScript data Message-ID: I have a web page which has a JavaScript defining some data in a form: var Categories = [ {name:"Cat1 ", caption:escape("Category 1")}, {name:"Cat2 ", caption:escape("Category 2")} ]; var DB = new Object(); DB[Categories[0].name] = [ {name:escape("Item1"), item-num:8} ]; etc... When I try to parse it with cl-json (using decode-json-from-string), I got errors because it can't accept things like variable definitions. Can I use cl-json to create a CL data from the JS data I mentioned or I should use some other tool? Thank you, Andrew -------------- next part -------------- An HTML attachment was scrubbed... URL: From henrik at evahjelte.com Tue Jun 10 07:01:40 2008 From: henrik at evahjelte.com (Henrik Hjelte) Date: Tue, 10 Jun 2008 09:01:40 +0200 Subject: [cl-json-devel] Parsing JavaScript data In-Reply-To: References: Message-ID: <50e8e4f60806100001n630ae245r3578a63eacf38168@mail.gmail.com> Hmm, what exactly are you parsing? Javascript? No in that case it can't be parsed. Not that json is not javascript, it is a subset of the javascript object notation. What exaclty are you sending, in a string, to lisp? Categories is an array, and DB is an object, so both have to be encoded to json. I would use some json client side library for that purpose. They are all around, in Dojo or Prototype or stand alone. Best wishes, Henrik Hjelte On Tue, Jun 10, 2008 at 12:46 AM, Andrei Stebakov wrote: > I have a web page which has a JavaScript defining some data in a form: > > var Categories = [ > {name:"Cat1 ", caption:escape("Category 1")}, > {name:"Cat2 ", caption:escape("Category 2")} > > ]; > > var DB = new Object(); > DB[Categories[0].name] = [ > {name:escape("Item1"), item-num:8} > ]; > > etc... > > > When I try to parse it with cl-json (using decode-json-from-string), I got > errors because it can't accept things like variable definitions. > Can I use cl-json to create a CL data from the JS data I mentioned or I > should use some other tool? > > Thank you, > Andrew > > > _______________________________________________ > cl-json-devel mailing list > cl-json-devel at common-lisp.net > http://common-lisp.net/cgi-bin/mailman/listinfo/cl-json-devel > > -- Henrik Hjelte henrik.hjelte at stix.to +46703993945 http://stix.to L?stmakarg 18-20 (IQube) Box 7438 S-103 91 Stockholm Sweden From lispercat at gmail.com Tue Jun 10 12:00:07 2008 From: lispercat at gmail.com (Andrei Stebakov) Date: Tue, 10 Jun 2008 08:00:07 -0400 Subject: [cl-json-devel] Parsing JavaScript data In-Reply-To: <50e8e4f60806100001n630ae245r3578a63eacf38168@mail.gmail.com> References: <50e8e4f60806100001n630ae245r3578a63eacf38168@mail.gmail.com> Message-ID: I was sending the string I mentioned starting with "var Categories = ....". Looks like I somehow need to translate the Categories and DB to some json tree, but I don't know how to do it. For output I need a lisp list/tree structure describing the same data something like '(:category "Cat1" (:name "Item1" :item-num 8) (:name "Item2" :iten-num 9)). Should I resort to flex/bison or I can just use the libraries available for common lisp? Thank you, Andrew On 6/10/08, Henrik Hjelte wrote: > > Hmm, what exactly are you parsing? > Javascript? No in that case it can't be parsed. Not that json is not > javascript, it is a subset of the javascript object notation. > > What exaclty are you sending, in a string, to lisp? > > Categories is an array, and DB is an object, so both have to be > encoded to json. I would use some json client side library for that > purpose. They are all around, in Dojo or Prototype or stand alone. > > Best wishes, > Henrik Hjelte > > On Tue, Jun 10, 2008 at 12:46 AM, Andrei Stebakov > wrote: > > > I have a web page which has a JavaScript defining some data in a form: > > > > var Categories = [ > > {name:"Cat1 ", caption:escape("Category 1")}, > > {name:"Cat2 ", caption:escape("Category 2")} > > > > ]; > > > > var DB = new Object(); > > DB[Categories[0].name] = [ > > {name:escape("Item1"), item-num:8} > > ]; > > > > etc... > > > > > > When I try to parse it with cl-json (using decode-json-from-string), I > got > > errors because it can't accept things like variable definitions. > > Can I use cl-json to create a CL data from the JS data I mentioned or I > > should use some other tool? > > > > Thank you, > > Andrew > > > > > > > _______________________________________________ > > cl-json-devel mailing list > > cl-json-devel at common-lisp.net > > http://common-lisp.net/cgi-bin/mailman/listinfo/cl-json-devel > > > > > > > > -- > Henrik Hjelte > henrik.hjelte at stix.to > +46703993945 > http://stix.to > L?stmakarg 18-20 (IQube) > Box 7438 > S-103 91 Stockholm > Sweden > _______________________________________________ > 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: From henrik at evahjelte.com Tue Jun 10 14:04:57 2008 From: henrik at evahjelte.com (Henrik Hjelte) Date: Tue, 10 Jun 2008 16:04:57 +0200 Subject: [cl-json-devel] Parsing JavaScript data In-Reply-To: References: <50e8e4f60806100001n630ae245r3578a63eacf38168@mail.gmail.com> Message-ID: <50e8e4f60806100704i4fd0ef92o895cff5a3dc64df@mail.gmail.com> On Tue, Jun 10, 2008 at 2:00 PM, Andrei Stebakov wrote: > I was sending the string I mentioned starting with "var Categories = ....". ok, that was javascript source code, not json. When it is evaluated, it becomes a javascript array. That array can be encoded to Json, for that you need a javascript library to do the encoding. Look at www.json.org for a description of json and for available libraries, I believe that all major javascript frameworks have a json encoder. For example if you have prototype it adds a method to all arrays: var hello=[{a:2,b:3}]; console.log(hello.toJSON()); >>> var hello=[{a:2,b:3}]; console.log(hello.toJSON()); [{"a": 2, "b": 3}] (output from Firebug, not the quotes around a and b when it is JSON) > Looks like I somehow need to translate the Categories and DB to some json > tree, but I don't know how to do it. I would look at the test cases for cl-json, they explain fairly well how to do encoding and decoding. > Should I resort to flex/bison or I can just use the libraries > available for common lisp? I don't know anything about flex or bison. But I say it is early to give up at this state, so far the problem has not been with cl-json. Good luck, Henrik From lispercat at gmail.com Tue Jun 10 14:41:16 2008 From: lispercat at gmail.com (Andrei Stebakov) Date: Tue, 10 Jun 2008 10:41:16 -0400 Subject: [cl-json-devel] Parsing JavaScript data In-Reply-To: <50e8e4f60806100704i4fd0ef92o895cff5a3dc64df@mail.gmail.com> References: <50e8e4f60806100001n630ae245r3578a63eacf38168@mail.gmail.com> <50e8e4f60806100704i4fd0ef92o895cff5a3dc64df@mail.gmail.com> Message-ID: I looked at the test cases for encoding/decoding. Looks like they all deal with json data structures starting with "[" and I couldn't find any method that would do anything similar to what you mentioned: >>> var hello=[{a:2,b:3}]; console.log(hello.toJSON()); [{"a": 2, "b": 3}] Did you suggest that I should find some library that would help me to evaluate my var hello=[{a:2,b:3}]; to [{"a": 2, "b": 3}] and then use cl-json to decode it to alist? Andrew On Tue, Jun 10, 2008 at 10:04 AM, Henrik Hjelte wrote: > On Tue, Jun 10, 2008 at 2:00 PM, Andrei Stebakov > wrote: > > I was sending the string I mentioned starting with "var Categories = > ....". > > ok, that was javascript source code, not json. > When it is evaluated, it becomes a javascript array. > That array can be encoded to Json, for that you need > a javascript library to do the encoding. > Look at www.json.org for a description of json and for available > libraries, I believe that > all major javascript frameworks have a json encoder. > > For example if you have prototype it adds a method to all arrays: > > var hello=[{a:2,b:3}]; > console.log(hello.toJSON()); > > >>> var hello=[{a:2,b:3}]; console.log(hello.toJSON()); > [{"a": 2, "b": 3}] > (output from Firebug, not the quotes around a and b when it is JSON) > > > Looks like I somehow need to translate the Categories and DB to some json > > tree, but I don't know how to do it. > > I would look at the test cases for cl-json, they explain fairly well > how to do encoding > and decoding. > > > Should I resort to flex/bison or I can just use the libraries > > available for common lisp? > > I don't know anything about flex or bison. But I say it is early to > give up at this state, > so far the problem has not been with cl-json. > > Good luck, > Henrik > -------------- next part -------------- An HTML attachment was scrubbed... URL: