The z3c.json write implementation uses str to convert any unrecognized
data types json. But json only recogonizes basic data types, and
strings must be enclosed in quotation marks, so the data
representation given by z3c.json is often invalid. For example:

  >>> import zope.component
  >>> from z3c.json import interfaces
  >>> from z3c.json import testing
  >>> testing.setUpJSONConverter()
  >>> jsonWriter = zope.component.getUtility(interfaces.IJSONWriter)
  >>> from datetime import date
  >>> input = {u'date': date.today()}
  >>> jsonWrite.write(input)
  u'{"date":2008-01-31}'

This string would be evaluate by most javascript implementations to
give the result of {date: 1976}, since 2008-01-31 is not quoted and is
regarded as an expression by javascript eval function.

I think there is a bug in the minjson.py, which uses str function to
convert an unrecognized type to json string but without quoting the
return value:

           ....
            # if we are not baseobj, convert to it
            try:
                obj = str(obj)
            except Exception, exc:
                raise WriteException, 'Cannot write object (%s: %s)' %
(exc.__class__, exc)
            self.stream.write(obj)

Shouldn't the code read better as?

            # if we are not baseobj, convert to it
            try:
                obj = str(obj)
            except Exception, exc:
                raise WriteException, 'Cannot write object (%s: %s)' %
(exc.__class__, exc)
            self.stream.write('"')
            self.stream.write(obj)
            self.stream.write('"')

-- 
Hong Yuan

大管家网上建材超市
装修装潢建材一站式购物
http://www.homemaster.cn
_______________________________________________
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users

Reply via email to