From jim.barrows at gmail.com Thu Apr 7 14:07:18 2011 From: jim.barrows at gmail.com (Jim Barrows) Date: Thu, 7 Apr 2011 07:07:18 -0700 Subject: [cl-json-devel] Newbie to Lisp question, trying to create JSON Message-ID: I'm trying to encode a list of plists, and encode-json-plist-to-string doesn't appear to do it. My newbiness is not seeing a better answer then to loop through the list, and encode each plist separately, concatentate the strings together to form the array. That doesn't seem too lispy to me. Is there a better way? -- James A Barrows -------------- next part -------------- An HTML attachment was scrubbed... URL: From kentilton at gmail.com Thu Apr 7 14:28:38 2011 From: kentilton at gmail.com (Ken Tilton) Date: Thu, 07 Apr 2011 10:28:38 -0400 Subject: [cl-json-devel] Newbie to Lisp question, trying to create JSON In-Reply-To: References: Message-ID: <4D9DCA16.6050601@gmail.com> On 4/7/2011 10:07 AM, Jim Barrows wrote: > I'm trying to encode a list of plists, and encode-json-plist-to-string > doesn't appear to do it. > My newbiness is not seeing a better answer then to loop through the > list, and encode each plist separately, concatentate the strings > together to form the array. That doesn't seem too lispy to me. > Is there a better way? I am more interested in "doesn't appear to do it". How about a small example of input and output? kt From rpgoldman at sift.info Thu Apr 7 14:13:01 2011 From: rpgoldman at sift.info (Robert Goldman) Date: Thu, 07 Apr 2011 09:13:01 -0500 Subject: [cl-json-devel] Newbie to Lisp question, trying to create JSON In-Reply-To: References: Message-ID: <4D9DC66D.4060202@sift.info> On 4/7/11 Apr 7 -9:07 AM, Jim Barrows wrote: > I'm trying to encode a list of plists, and encode-json-plist-to-string > doesn't appear to do it. > My newbiness is not seeing a better answer then to loop through the > list, and encode each plist separately, concatentate the strings > together to form the array. That doesn't seem too lispy to me. > Is there a better way? >From the outside, it would help if you were to provide an example of what you want to encode, what you have tried, and what you got. That will help list members provide better support. cheers, r From henrik at evahjelte.com Thu Apr 7 20:39:20 2011 From: henrik at evahjelte.com (Henrik Hjelte) Date: Thu, 7 Apr 2011 22:39:20 +0200 Subject: [cl-json-devel] Newbie to Lisp question, trying to create JSON In-Reply-To: References: Message-ID: On Thu, Apr 7, 2011 at 4:07 PM, Jim Barrows wrote: > I'm trying to encode a list of plists, and encode-json-plist-to-string > doesn't appear to do it. Yes, because the first list is a list and not a plist. > My newbiness is not seeing a better answer then to loop through the list, > and encode each plist separately, concatentate the strings together to form > the array. > If you go that route, I would rather wrap a string-stream with with-output-to-string and encode each individual plist to this stream (not the -to-string functions) to avoid string concatenation at the end. > That doesn't seem too lispy to me. > Is there a better way? > I suggest using the explicit encoder for more control, but then you would need to change your p-lists slightly. I assume you want the plists to be json objects, and the list to be a json array. Both these examples give the same result: [{"a":"c","d":"c"},{"foo":true,"bar":false} ;; More json-javascriptish terminolgy (princ (json:with-explicit-encoder (json:encode-json-to-string '(:array (:object :a "c" :d "c") (:object :foo t :bar (:false)))))) ;; More lispish terminology (princ (json:with-explicit-encoder (json:encode-json-to-string '(:list (:plist :a "c" :d "c") (:plist :foo t :bar (:false)))))) Or you could use the streaming encoder. /Henrik Hjelte -------------- next part -------------- An HTML attachment was scrubbed... URL: From fbogdanovic at xnet.hr Fri Apr 8 15:39:32 2011 From: fbogdanovic at xnet.hr (Haris) Date: Fri, 8 Apr 2011 17:39:32 +0200 Subject: [cl-json-devel] installation Message-ID: <37C379FCD9134F1B9725E85515B6A225@komp> Hi. I did (asdf:oos 'asdf:load-op 'cl-json) but I get: Package not found error in Lispworks (when I write cl-json: and hit tab in repl) ? What do I write in command line (Windows) to get the Darcs version ? Thanks From henrik at evahjelte.com Fri Apr 8 18:44:06 2011 From: henrik at evahjelte.com (Henrik Hjelte) Date: Fri, 8 Apr 2011 20:44:06 +0200 Subject: [cl-json-devel] installation In-Reply-To: <37C379FCD9134F1B9725E85515B6A225@komp> References: <37C379FCD9134F1B9725E85515B6A225@komp> Message-ID: On Fri, Apr 8, 2011 at 5:39 PM, Haris wrote: > > Hi. > > I did > > (asdf:oos 'asdf:load-op 'cl-json) > > but I get: Package not found error in Lispworks (when I write cl-json: and > hit tab in repl) ? I don't have lispworks to test with, but this probably means that the file cl-json.asd is not in one of the directories in asdf:*central-registry*. To make it available, do something like this (pushnew #p"c:\mylisplibs\cl-json\" asdf:*central-registry*) I am pretty sure this is not a problem with cl-json, even an old version should work. > > What do I write in command line (Windows) to get the Darcs version ? If you have installed a windows version of darcs, it is: darcs get http://common-lisp.net/project/cl-json/darcs/cl-json But: I think it is easier to use quicklisp, it should solve these kinds of problems. /Henrik Hjelte From fbogdanovic at xnet.hr Fri Apr 8 21:34:12 2011 From: fbogdanovic at xnet.hr (Haris) Date: Fri, 8 Apr 2011 23:34:12 +0200 Subject: [cl-json-devel] ajax Message-ID: <732FE6AFF7064741A5F14CDCF950E949@komp> Hi. How to do ajax with cl-json on Hunchentoot ? Can you give me a simple example, calling a lisp function from javascript ? Thanks From henrik at evahjelte.com Fri Apr 8 22:38:04 2011 From: henrik at evahjelte.com (Henrik Hjelte) Date: Sat, 9 Apr 2011 00:38:04 +0200 Subject: [cl-json-devel] ajax In-Reply-To: <732FE6AFF7064741A5F14CDCF950E949@komp> References: <732FE6AFF7064741A5F14CDCF950E949@komp> Message-ID: On Fri, Apr 8, 2011 at 11:34 PM, Haris wrote: > > Hi. > > How to do ajax with cl-json on Hunchentoot ? > Can you give me a simple example, > calling a lisp function from javascript ? Something like this: (hunchentoot:define-easy-handler (hello :uri "/hello") (name) ;; call with /hello?name=foo (setf (hunchentoot:content-type*) "application/json") (json:encode-json-plist-to-string `(:msg ,(format nil "Hello ~a" name)))) Disclaimer: Untested.. /Henrik From henrik at evahjelte.com Tue Apr 12 07:42:18 2011 From: henrik at evahjelte.com (Henrik Hjelte) Date: Tue, 12 Apr 2011 09:42:18 +0200 Subject: [cl-json-devel] Newbie to Lisp question, trying to create JSON In-Reply-To: References: Message-ID: >> On Thu, Apr 7, 2011 at 4:07 PM, Jim Barrows wrote: >>> >>> I'm trying to encode a list of plists, and encode-json-plist-to-string >>> doesn't appear to do it. >> >> Yes, because the first list is a list and not a plist. > > > Am I incorrect in thinking this is a list of plists? ?I'm still in shock > over the difference in terminology between Java and Lisp... And I am often shocked when I need to do various things in the Java world, so I understand your reaction. No your right, it is a list of p-lists. But the encode-json-plist function is for p-lists, not necessarily lists of p-lists. It is an easy way to render a simple key-value object. Probably not the best choice if you want to have control over details, then I suggest the explicit encoder. The problem is for the encoder to guess what you intend to have as output. A p-list looks just like a list, so what do you want as output, an object or a list? How should a nested list (one of the values in the key-value pair) that looks like a p-list be converted, to a json-list or a json-object? It is slightly easier with a-lists, I think you will get a list of objects if you have a list of a-lists and encode-json-alist. Or if you don't like the unpredictable ways of some code trying to guess what you want, just use the explicit encoder or the streaming encoder, then you have full control. Look at the testcases for lots of examples. /Henrik From kentilton at gmail.com Tue Apr 12 09:58:09 2011 From: kentilton at gmail.com (Ken Tilton) Date: Tue, 12 Apr 2011 05:58:09 -0400 Subject: [cl-json-devel] Newbie to Lisp question, trying to create JSON In-Reply-To: References: Message-ID: <4DA42231.40804@gmail.com> On 4/12/2011 3:42 AM, Henrik Hjelte wrote: >>> On Thu, Apr 7, 2011 at 4:07 PM, Jim Barrows wrote: >>>> I'm trying to encode a list of plists, and encode-json-plist-to-string >>>> doesn't appear to do it. >>> Yes, because the first list is a list and not a plist. >> >> Am I incorrect in thinking this is a list of plists? I'm still in shock >> over the difference in terminology between Java and Lisp... Uh-oh. The difference in semantics might kill you. Enjoy the ride. :) kt From fbogdanovic at xnet.hr Tue Apr 12 21:28:20 2011 From: fbogdanovic at xnet.hr (Haris) Date: Tue, 12 Apr 2011 23:28:20 +0200 Subject: [cl-json-devel] jsonp Message-ID: <011169CDF61641E281E811F62D829E84@komp> Hi. Can someone give me an example of using jsonp ? For example, I have a lisp function: (defun add (x y) (+ x y)) and I want to call it from javascript: add (2, 2); How do I do that ? Thanks From henrik at evahjelte.com Wed Apr 13 15:05:25 2011 From: henrik at evahjelte.com (Henrik Hjelte) Date: Wed, 13 Apr 2011 17:05:25 +0200 Subject: [cl-json-devel] jsonp In-Reply-To: <011169CDF61641E281E811F62D829E84@komp> References: <011169CDF61641E281E811F62D829E84@komp> Message-ID: On Tue, Apr 12, 2011 at 11:28 PM, Haris wrote: > Hi. > > Can someone give me an example of using jsonp ? > For example, I have a lisp function: > (defun add (x y) > ? ?(+ x y)) > and I want to call it from javascript: > add (2, 2); > How do I do that ? It depends on the web-server. Here are some steps: 1. You need to define a query parameter that specifies the name of the callback javascript function. Popular choices are "callback" or "jsoncallback" 2. You need to set the response-type to application/javascript since it is not really json you return but a small javascript function call. 3. You need to get the x and y parameter, call add and generate a json-reponse. If the actual example is the add function the json equivalent to 4 is just 4 so you don't even need cl-json. 4. The json-reponse 4 should be made to look as a javascript function call, wrapped in parenthesis, end with a semicolon and have the callback function prepended. If you call mywebserver?fn=add&x=2&y=2&callback=foo you should generate this string as an answer: foo(4); And again served as application/javascript. For hunchentoot, an untested implementation, not optimized for efficiency or safety: (hunchentoot:define-easy-handler (jsonp-sample :uri "/add") (callback x y) (setf (hunchentoot:content-type*) "application/javascript") (let ((answer (+ x y))) (format nil "~a(~a);" callback (json:encode-json-to-string answer)))) I would start to test with an even easier version, with "alert" as a predefined callback and no x and y and no json-encode to see if you get hunchentoot running and get a response that works, then take it from there. Hope it helps, /Henrik