Rails:漂亮的打印JSON没有过度杀伤力

Flo*_*ock 14 ruby-on-rails ruby-on-rails-4

我正在使用以下代码在JSON中显示未经授权的消息:

def render_unauthorized
  # Displays the Unauthorized message since the user did
  # not pass proper authentication parameters.
  self.headers['WWW-Authenticate'] = 'Token realm="API"'
  render json: { 
    error: { 
      type: "unauthorized",
      message: "This page cannot be accessed without a valid API key." 
      } 
    }, status: 401
end
Run Code Online (Sandbox Code Playgroud)

哪个输出:

{"error":{"type":"unauthorized","message":"如果没有有效的API密钥,则无法访问此页面."}}

所以我的问题是:有没有办法漂亮地打印这条消息(没有把它放在一个单独的视图中并使用一些第三方宝石).

编辑:什么是漂亮的印刷品?

适当间隔,好......漂亮.这是我想看到的输出:

{
  "error": {
    "type": "unauthorized",
    "message": "This page cannot be accessed without a valid API key." 
  }
}
Run Code Online (Sandbox Code Playgroud)

使用@ emaillenin的答案可以解决问题.为了记录,这是最终代码的样子(因为他没有包括整个代码):

def render_unauthorized
  # Displays the Unauthorized message since the user did
  # not pass proper authentication parameters.
  self.headers['WWW-Authenticate'] = 'Token realm="API"'
  render json: JSON.pretty_generate({ # <-- Here it is :')
    error: { 
      type: "unauthorized",
      message: "This page cannot be accessed without a valid API key." 
      } 
    }), status: 401
end
Run Code Online (Sandbox Code Playgroud)

Len*_*ran 26

使用JSON中内置的辅助方法.

JSON.pretty_generate
Run Code Online (Sandbox Code Playgroud)

我只是尝试了它的工作原理:

> str = '{"error":{"type":"unauthorized","message":"This page cannot be accessed without a valid API key."}}'
> JSON.pretty_generate(JSON.parse(str))
 => "{\n  \"error\": {\n    \"type\": \"unauthorized\",\n    \"message\": \"This page cannot be accessed without a valid API key.\"\n  }\n}"
Run Code Online (Sandbox Code Playgroud)

  • 甜的!使用“render json: JSON.pretty_generate(...)”就像一个魅力。现在我需要回去做真正重要的事情:/ (2认同)

Phr*_*ogz 9

如果你(像我一样)发现pretty_generateRuby的JSON库中内置的选项不够"漂亮",我推荐自己的NeatJSONgem用于格式化.

要使用它gem install neatjson然后使用JSON.neat_generate而不是JSON.pretty_generate.

像Ruby一样,pp它会在适合的时候将对象和数组放在一行上,但根据需要包装到多个.例如:

{
  "navigation.createroute.poi":[
    {"text":"Lay in a course to the Hilton","params":{"poi":"Hilton"}},
    {"text":"Take me to the airport","params":{"poi":"airport"}},
    {"text":"Let's go to IHOP","params":{"poi":"IHOP"}},
    {"text":"Show me how to get to The Med","params":{"poi":"The Med"}},
    {"text":"Create a route to Arby's","params":{"poi":"Arby's"}},
    {
      "text":"Go to the Hilton by the Airport",
      "params":{"poi":"Hilton","location":"Airport"}
    },
    {
      "text":"Take me to the Fry's in Fresno",
      "params":{"poi":"Fry's","location":"Fresno"}
    }
  ],
  "navigation.eta":[
    {"text":"When will we get there?"},
    {"text":"When will I arrive?"},
    {"text":"What time will I get to the destination?"},
    {"text":"What time will I reach the destination?"},
    {"text":"What time will it be when I arrive?"}
  ]
}
Run Code Online (Sandbox Code Playgroud)

它还支持各种格式化选项,以进一步自定义输出.例如,冒号之前/之后有多少个空格?逗号之前/之后?在数组和对象的括号内?你想对对象的键进行排序吗?你想要将所有冒号排成一列吗?

使用该aligned选项将允许您的输出如下所示:

{
  "error": {
    "type"    : "unauthorized",
    "message" : "This page cannot be accessed without a valid API key." 
  }
}
Run Code Online (Sandbox Code Playgroud)