From david.cooper at genworks.com Sun Jan 13 18:47:31 2013 From: david.cooper at genworks.com (Dave Cooper) Date: Sun, 13 Jan 2013 13:47:31 -0500 Subject: [cl-json-devel] Achieving round-tripping for symbols with cl-json Message-ID: Hi All, Sorry if this has been covered, I am a bit late to the party on the mailing list here... anyway: is there a conventional way to achieve round-tripping of e.g. plists with cl-json? For example: CL-USER> (setq obj (list :|iid| NIL :|bashee| (list :|%rp%| NIL))) (:|iid| NIL :|bashee| (:|%rp%| NIL)) CL-USER> (json:encode-json-to-string obj) "[\"iid\",null,\"bashee\",[\"%rp%\",null]]" CL-USER> (json:decode-json-from-string (json:encode-json-to-string obj)) ("iid" NIL "bashee" ("%rp%" NIL)) At the end here, what I really want is the identical plist back: (:|iid| NIL :|bashee| (:|%rp%| NIL)) The CL-JSON documentation is clear that symbols in Lisp go to strings in JSON, which come back as strings in Lisp --- but is there any general, conventional technique people are using on top of this to preserve printable Lisp objects (mainly I'm talking about symbols here) through round-trips Lisp->JSON->Lisp? Up until now we have been using base64 encoding for this kind of thing: (base64-encode (format nil "~s" obj)) to get the Lisp stuff into a format for the web page which can be passed through e.g. as the parameter for an Ajax call, then (read-from-string (base64-decode ...)) back in Lisp to get it back and usable in Lisp. Putting everything into a base64 encoded string avoids problems with having to escape characters or other weirdness caused by strange characters which can confuse JavaScript. But this gets to be kind of cryptic (it makes the web page source code pretty much unreadable for humans), and I am learning that it starts failing for UTF-8 characters, etc, unless we are very careful about our base64 encoding. So I am hoping to standardize on JSON for this kind of thing, if there is a generally accepted way to do round-tripping. Plus I want to use json for scraping specific form-control values from a web page through Ajax (right now we have god-awful Javascript with all kinds of crazy character-escaping workarounds to do this). But I think that is another topic... Anyway am I barking up the wrong tree here trying to get round-tripping with cl-json? Regards, Dave -- My Best, Dave Cooper, Genworks Support david.cooper at genworks.com, dave.genworks.com(skype) USA: 248-327-3253(o), 1-248-330-2979(mobile) UK: 0191 645 1699 -------------- next part -------------- An HTML attachment was scrubbed... URL: From rpgoldman at sift.info Sun Jan 13 21:56:56 2013 From: rpgoldman at sift.info (Robert Goldman) Date: Sun, 13 Jan 2013 15:56:56 -0600 Subject: [cl-json-devel] Achieving round-tripping for symbols with cl-json In-Reply-To: References: Message-ID: <50F32DA8.4020409@sift.info> As far as I can tell, there's no way for CL-JSON to know whether something was intended to be a string or a symbol (and, if a symbol, what package). I can think of two possible solutions: 1. if it's specifically plist keys you are interested in, somehow mark or know when an array is a plist. Then simply decode all the keys as symbols (in the keyword package?) and all the values as normal. 2. More elaborate: set up a special kind of JSON object that will capture the information about symbols, as appropriate. E.g.,: { type: "lispSymbol", package: "pkg-name", name: "symbolName" } and decode that as 'pkg-name:|symbolName| That's probably the most robust way to do this. cheers, r From rsx11 at ro.ru Tue Jan 22 07:28:15 2013 From: rsx11 at ro.ru (=?koi8-r?B?78zFxw==?=) Date: Tue, 22 Jan 2013 11:28:15 +0400 Subject: [cl-json-devel] Slot name with digits raise condition UNBOUND-SLOT Message-ID: <1358839695.5581.5290.10029@saddam4.rambler.ru> (json:with-decoder-simple-clos-semantics (let ((json:*json-symbols-package* nil)) (let ((x (json:decode-json-from-string "{\"foo1\": [1, 2, 3], \"bar1\": true, \"baz1\": \"!\"}"))) (with-slots (foo1 bar1 baz1) x (values x foo1 bar1 baz1))))) The slot FOO1 is unbound in the object #<# {1003CAFB43}>. [Condition of type UNBOUND-SLOT] ????. -------------- next part -------------- An HTML attachment was scrubbed... URL: From boris.smilga at gmail.com Tue Jan 22 18:37:13 2013 From: boris.smilga at gmail.com (Boris Smilga) Date: Tue, 22 Jan 2013 22:37:13 +0400 Subject: [cl-json-devel] Slot name with digits raise condition UNBOUND-SLOT In-Reply-To: <1358839695.5581.5290.10029@saddam4.rambler.ru> References: <1358839695.5581.5290.10029@saddam4.rambler.ru> Message-ID: <2E01A0EC-AC2E-45C7-8CDB-D20A35C2B212@gmail.com> On 22 Jan 2013, at 11:28, ???? wrote: > (json:with-decoder-simple-clos-semantics > (let ((json:*json-symbols-package* nil)) > (let ((x (json:decode-json-from-string > "{\"foo1\": [1, 2, 3], \"bar1\": true, > \"baz1\": \"!\"}"))) > (with-slots (foo1 bar1 baz1) x > (values x foo1 bar1 baz1))))) > > > The slot FOO1 is unbound in the object #<# {1004085F33}> > {1003CAFB43}>. > [Condition of type UNBOUND-SLOT] > When Lisp slot names are derived from JSON object keys, they are transcribed to more Lisp-like conventions: camel case is replaced with hyphenation, all caps become framing asterisks, etc. So, slot names in your Lisp code should be written with hyphens: (json:with-decoder-simple-clos-semantics (let ((json:*json-symbols-package* nil)) (let ((x (json:decode-json-from-string "{\"foo1\": [1, 2, 3], \"bar1\": true, \"baz1\": \"!\"}"))) (with-slots (foo-1 bar-1 baz-1) x (values x foo-1 bar-1 baz-1))))) => #<# {5A1ECF11}> => #(1 2 3) => T =>"!" If this disagrees too much with the conventions of your code, you can change the way identifiers are transcribed by setting / rebinding the variable *JSON-IDENTIFIER-NAME-TO-LISP* to the appropriate transcriber function. Hope this helps. ? B. Smilga. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rsx11 at ro.ru Wed Jan 23 07:19:35 2013 From: rsx11 at ro.ru (=?koi8-r?B?78zFxw==?=) Date: Wed, 23 Jan 2013 11:19:35 +0400 Subject: [cl-json-devel] Slot name with digits raise condition UNBOUND-SLOT Message-ID: <1358925575.123116.27835.65104@saddam3.rambler.ru> Thanks, Boris. ????. -------------- next part -------------- An HTML attachment was scrubbed... URL: