Unicode/UTF-8 confusion

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Sun Mar 16 02:21:45 EDT 2008


On Sat, 15 Mar 2008 16:33:24 -0400, Tom Stambaugh wrote:

> I use a trick to let me pass the information into my browser client 
> application. The browser requests the server information from a form whose 
> target is a hidden iframe. The string the server serializes is wrapped in 
> html that embeds it in an onload handler like this:
> 
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml"><head><meta 
> http-equiv="content-type" content="text/html; charset=UTF-8" /><script 
> type="text/javascript">//<![CDATA[
> function vpage_load() {
>     var aParent = window.parent;
>     if (!aParent || !aParent.document || !aParent.document.vpage) {
>         alert("No parent, parent.document, or parent.document.vpage");
>         return;}
>     var aSerializedObject = '%(jsonString)s';
>     if (aParent && aParent._clientApplication) {
>         aParent._clientApplication.loadObject(aSerializedObject, 
> window.document, '' + window.document.location, true)}
>     else {
>         alert("No parent or no clientApplication")}
>     }
>     //]]></script>
>     </head>
>     <body onload="vpage_load();">
>         <input id="state" value="" type="text">
>         <textarea id="vpage"></textarea>
>         </body></html>
> 
> […]
> 
> In order to successfully pass the escapes to the server, I already have to 
> double any each backslash. At the end of the day, it's easier -- and results 
> in better performance -- to convert each apostrophe to its unicode 
> equivalent, as I originally asked.
> 
> I just want to know if there's a faster way to persuade simplejson to 
> accomplish the feat.

So you don't ask for JSON encoded objects but JSON encoded *and*
HTML/JavaScript embeddable escaped literal string.  That's simply not the
job of a JSON encoder.  That's another level of encoding/escaping
producing something that is not JSON anymore, so why do you want to ask a
JSON encoder to deliver it?

This is a feature/function you should find in a HTML templating library.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list