Rails 为什么 github API 返回的格式那么好看?

yakjuly · October 31, 2013 · Last by yakjuly replied at October 31, 2013 · 4756 hits

我用 grape 提供 API 返回的 json 在浏览器上一团乱,但是 github 的 API 在浏览器上看起来很舒服。 有什么办法 让 grape 返回的 API 也像 pretty_inspect 一样吗?

gzip 了没?

和 gzip 没关系。

你可以装浏览器插件,格式化 JSON 数据

改了 grape 的默认 json formatter 后好看多了。

Grape::Formatter::Json.instance_eval <<-EOF
  def call(object, env)
    if object.is_a?(Hash)
      JSON.pretty_generate(object)
    else
      if object.respond_to?(:to_json)
        j = object.to_json 
        JSON.pretty_generate( JSON.parse(j))
      else
        JSON.pretty_generate( object)
      end
    end
  end
EOF

#5 楼 @yakjuly 是的,可以参考下 https://github.com/intridea/grape#api-formats ,自己写一个 formatter 然后 formatter :json, PrettyJsonFormatter。这样显得不那么 hacky...

@WolfLee 第二步,就是改成 PrettyJsonFormatter

You need to Sign in before reply, if you don't have an account, please Sign up first.