[Twisted-web] [Nevow] json serializer and strings

Manlio Perillo manlio_perillo at libero.it
Sun Sep 10 04:04:03 CDT 2006

Cory Dodt ha scritto:
> On 9/9/06, *Manlio Perillo* <manlio_perillo at libero.it
> <mailto:manlio_perillo at libero.it>> wrote:
>     >> There are some problems with this:
>     >>
>     >>    elif isinstance(obj, str):
>     >>        w('"')
>     >>        w(stringEncode(obj.decode("us-ascii"))
>     >>        w('"')
>     >> ?
>     >
>     > Yes.  What if it is not an ASCII string?
>     Raise an arror?
>     Is this really a problem?
> Yes.  Yes it is.  Javascript strings are unicode.  Therefore the
> implementation must be able to convert the encoded string (byte
> representation) into Unicode when it arrives.
> In order to convert the parameter to unicode, the API has to know what
> encoding the original string was; or it must have it in Unicode form
> already.  If the API accepts 8-bit str objects, then it must guess at
> the encoding to produce a unicode object.  It will guess wrong very
> often, which leads to bugs.  Therefore, it does not accept 8-bit str
> objects.

I don't agree, but that's not a problem; I just can decode the keywords
dictionary by myself (or use another library).

However you say:
"it does not accept 8-bit str objects".

Well, all I can say is that by doing obj.decode("us-ascii") we are
accepting 7-bit str objects!

If you like:

except UnicodeDecodeError:
  raise ValueError("8-bit strings not supported")

This implementation is just a little more friendly since str objects are
the default in CPython:

 >>> def foo(**kwargs):
 >>>  print type(kwargs.keys()[0])

 >>> foo(a=1)
 <type 'str'>

Regards  Manlio Perillo

More information about the Twisted-web mailing list