[Twisted-web] JSON "page" and nevow

Jean-Paul Calderone exarkun at divmod.com
Sat Aug 16 08:50:23 EDT 2008


On Sat, 16 Aug 2008 14:36:47 +0200, Vincent Bernat <bernat at luffy.cx> wrote:
>Hi!
>
>There are several  pages that I would like to serve  as JSON page. Nevow
>include a  JSON serializer but it seems  there is no facility  to send a
>page as JSON.

`nevow.json´ is a support module for `nevow.athena´.  It's not intended to
be a complete or totally awesome JSON implementation.

>
>Here is what I do:
>
>,----
>| class JsonPage(rend.Page):
>|
>|     docFactory = loaders.stan ( T.div(render=T.directive("json"),
>|                                       data=T.directive("json")))
>|     addSlash = True
>|     flattenFactory = lambda self, *args: flat.flattenFactory(*args)
>|
>|     def render_json(self, ctx, data):
> [snip]
>
>I inherit of this class and  just implement data_json. The main point is
>that I should be able what I do for regular web pages, including the use
>of deferred.

JSON isn't XHTML, and the `docFactory´ and `render_*´ mechanisms are
primarily concerned with the generation of XHTML.  To generate JSON,
I think you'd do better to ignore these features of Page and override
the `renderHTTP´ method instead.  You won't have to deal with < being
turned into &lt; and other such XHTML-related problems.

Hope this helps,

Jean-Paul



More information about the Twisted-web mailing list