{
"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?"}
]
}
class PrettyJsonResponse
def initialize(app)
@app = app
end
def call(env)
@status, @headers, @response = @app.call(env)
[@status, @headers, self]
end
def each(&block)
@response.each do |body|
if @headers["Content-Type"] =~ /^application\/json/
body = pretty_print(body)
end
block.call(body)
end
end
private
def pretty_print(json)
obj = JSON.parse(json)
JSON.pretty_unparse(obj)
end
end
class PrettyJson
def self.dump(object)
JSON.pretty_generate(object, {:indent => " "})
end
end
Rabl.configure do |config|
...
config.json_engine = PrettyJson if Rails.env.development?
...
end
ActiveSupport::TimeWithZone.class_eval do
alias_method :orig_to_s, :to_s
def to_s(format = :default)
format == :default ? iso8601 : orig_to_s(format)
end
end
ActionController::Renderers.add :json do |json, options|
unless json.kind_of?(String)
json = json.as_json(options) if json.respond_to?(:as_json)
json = JSON.pretty_generate(json, options)
end
if options[:callback].present?
self.content_type ||= Mime::JS
"#{options[:callback]}(#{json})"
else
self.content_type ||= Mime::JSON
json
end
end
2
# example of use:
a_hash = {user_info: {type: "query_service", e_mail: "my@email.com", phone: "+79876543322"}, cars_makers: ["bmw", "mitsubishi"], car_models: [bmw: {model: "1er", year_mfc: 2006}, mitsubishi: {model: "pajero", year_mfc: 1997}]}
pretty_html = a_hash.pretty_html
# include this module to your libs:
module MyPrettyPrint
def pretty_html indent = 0
result = ""
if self.class == Hash
self.each do |key, value|
result += "#{key} : #{[Array, Hash].include?(value.class) ? value.pretty_html(indent+1) : value} "
end
elsif self.class == Array
result = "[#{self.join(', ')}]"
end
"#{result}"
end
end
class Hash
include MyPrettyPrint
end
class Array
include MyPrettyPrint
end
16 回答
感谢Rack Middleware和Rails 3,您可以为每个请求输出漂亮的JSON,而无需更改应用程序的任何控制器 . 我已经写了这样的中间件片段,我在浏览器和
curl
输出中得到了很好的打印JSON .上面的代码应该放在你的Rails项目的
app/middleware/pretty_json_response.rb
中 . 最后一步是在config/environments/development.rb
中注册中间件:I don't recommend to use it in production.rb . JSON重新分析可能会降低 生产环境 应用程序的响应时间和吞吐量 . 最终可能会引入额外的逻辑,例如'X-Pretty-Json: true'标头,以按需触发手动卷曲请求的格式化 .
(使用Rails 3.2.8-5.0.0,Ruby 1.9.3-2.2.0,Linux测试)
将ActiveRecord对象转储到JSON(在Rails控制台中):
看看awesome_print . 将JSON字符串解析为Ruby Hash,然后使用awesome_print显示它,如下所示:
有了上述内容,您将看到:
awesome_print还会添加一些Stack Overflow不会显示的颜色:)
我使用以下内容,因为我发现 Headers ,状态和JSON输出有用作一个集合 . 呼叫例程根据railscasts演示文稿的推荐进行分析:http://railscasts.com/episodes/151-rack-middleware?autoplay=true
如果你(像我一样)发现Ruby的JSON库中内置的
pretty_generate
选项不够"pretty",我建议使用我自己的NeatJSON gem进行格式化 .要使用它
gem install neatjson
然后使用JSON.neat_generate
而不是JSON.pretty_generate
.就像Ruby的
pp
一样,当它们适合时它会将对象和数组保持在一条线上,但是根据需要包装到多个 . 例如:它还支持各种formatting options以进一步自定义您的输出 . 例如,冒号之前/之后有多少个空格?逗号之前/之后?在数组和对象的括号内?你想对对象的键进行排序吗?你想要将所有冒号排成一列吗?
HTML中的
<pre>
标记与JSON.pretty_generate
一起使用,将在您的视图中呈现JSON . 当我的杰出老板告诉我这件事时,我很高兴:使用
pretty_generate()
函数,该函数内置于更高版本的JSON中 . 例如:哪个让你:
使用
<pre>
html代码和pretty_generate
是一个好方法:这是从this excellent answer by @gertas修改的中间件解决方案 . 此解决方案不是Rails特定的 - 它应该适用于任何Rack应用程序 .
这里使用的中间件技术,使用#each,由Eifion Bedford在ASCIIcasts 151: Rack Middleware解释 .
此代码位于app / middleware / pretty_json_response.rb中:
要打开它,请将其添加到config / environments / test.rb和config / environments / development.rb:
正如@gertas在他的这个解决方案版本中警告的那样,避免在 生产环境 中使用它 . 这有点慢 .
用Rails 4.1.6测试 .
我使用了宝石CodeRay,效果很好 . 格式包括颜色,它识别许多不同的格式 .
我在一个可以用于调试rails API的gem上使用它,它运行得很好 .
顺便说一句,宝石名为'api_explorer'(http://www.github.com/toptierlabs/api_explorer)
如果您希望在Rails控制器操作中快速实现此操作以发送JSON响应:
这是我在自己的搜索过程中从其他帖子中获得的解决方案 .
这允许您根据需要将pp和jj输出发送到文件 .
如果您正在使用RABL,则可以按here所述配置它以使用JSON.pretty_generate:
使用JSON.pretty_generate的一个问题是JSON模式验证器将不再满足于您的日期时间字符串 . 您可以使用以下命令修复config / initializers / rabl_config.rb中的那些:
如果你想:
自动从您的应用程序中排除所有传出的JSON响应 .
避免污染Object#to_json / #as_json
避免使用中间件解析/重新呈现JSON(YUCK!)
用RAILS方式做吧!
然后......替换JSON的ActionController :: Renderer!只需将以下代码添加到ApplicationController: