【问题标题】:Is there a way to get the Pyramid json renderer to output formatted, pretty printed, output?有没有办法让 Pyramid json 渲染器输出格式化的、漂亮的打印输出?
【发布时间】:2013-08-03 01:44:59
【问题描述】:

我喜欢我的 json 输出格式很好,即使是 REST API。它在调试等时很有帮助。额外的开销很小,尤其是在使用 gzip 时

有没有办法告诉金字塔json渲染器(即这个东西)

    @view_config(request_method='POST', renderer='json')

输出格式化的、打印精美的输出?

【问题讨论】:

    标签: python json rest pyramid


    【解决方案1】:

    我只是自己想出来的。在我的 init 中,我添加了

     from pyramid.renderers import JSON
     # ...
     config.add_renderer('prettyjson', JSON(indent=4))
    

    然后我只是在我看来这样做

       @view_config(request_method='POST', renderer='prettyjson')
    

    【讨论】:

    • 更好的是,您可以通过稍微修改add_renderer call config.add_renderer('json', JSON(indent=4)) 覆盖现有的json 渲染器
    猜你喜欢
    • 2012-10-17
    • 1970-01-01
    • 2013-11-19
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多