[cl-json-devel] Achieving round-tripping for symbols with cl-json
Dave Cooper
david.cooper at genworks.com
Sun Jan 13 18:47:31 UTC 2013
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: <https://mailman.common-lisp.net/pipermail/cl-json-devel/attachments/20130113/55376a6c/attachment.html>
More information about the cl-json-devel
mailing list