كيف يمكنني "جميلة" تنسيق JSON الانتاج في روبي على القضبان ؟
-
01-07-2019 - |
سؤال
أود سلمان الانتاج في روبي على القضبان إلى أن "جميلة" أو تنسيق جيد.
الآن أعطي الكلمة to_json
و سلمان هو كل على سطر واحد.في بعض الأحيان هذا يمكن أن يكون من الصعب معرفة ما إذا كان هناك مشكلة في JSON تيار الإخراج.
هل هناك طريقة لتكوين أو طريقة لجعل بلدي سلمان "جميلة" أو تنسيق جيد في القضبان ؟
المحلول
استخدام pretty_generate()
وظيفة بنيت في الإصدارات الأحدث من سلمان.على سبيل المثال:
require 'json'
my_object = { :array => [1, 2, 3, { :sample => "hash"} ], :foo => "bar" }
puts JSON.pretty_generate(my_object)
الذي يحصل لك:
{
"array": [
1,
2,
3,
{
"sample": "hash"
}
],
"foo": "bar"
}
نصائح أخرى
شكرا على الرف الوسيطة و القضبان 3 يمكنك إخراج جميلة سلمان على كل طلب دون تغيير أي تحكم من التطبيق الخاص بك.كنت قد كتبت هذه الوسيطة مقتطف وأنا الحصول على مطبوع لطيف سلمان في المتصفح ، curl
الإخراج.
class PrettyJsonResponse
def initialize(app)
@app = app
end
def call(env)
status, headers, response = @app.call(env)
if headers["Content-Type"] =~ /^application\/json/
obj = JSON.parse(response.body)
pretty_str = JSON.pretty_unparse(obj)
response = [pretty_str]
headers["Content-Length"] = pretty_str.bytesize.to_s
end
[status, headers, response]
end
end
رمز أعلاه ينبغي أن توضع في app/middleware/pretty_json_response.rb
من القضبان المشروع.و الخطوة الأخيرة هي تسجيل الوسيطة في config/environments/development.rb
:
config.middleware.use PrettyJsonResponse
لا يوصي لاستخدامها في production.rb
.سلمان reparsing قد يقل زمن الاستجابة و الإنتاجية من الإنتاج الخاص بك التطبيق.في نهاية المطاف إضافية المنطق مثل 'X-جميلة-سلمان:صحيح' رأس قد تكون أدخلت على الزناد التنسيق اليدوي حليقة الطلبات على الطلب.
(اختبار مع القضبان 3.2.8-5.0.0, روبي 1.9.3-2.2.0, لينكس)
على <pre>
الوسم في HTML, تستخدم مع JSON.pretty_generate
, سوف تجعل JSON جدا في وجهة نظركم.كنت سعيدة جدا عندما اللامع مدرب أظهر لي هذا:
<% if @data.present? %>
<pre><%= JSON.pretty_generate(@data) %></pre>
<% end %>
إذا كنت ترغب في:
- تجميل الصادرة سلمان الردود من التطبيق الخاص بك تلقائيا.
- تجنب تلويث وجوه#to_json/#as_json
- تجنب تحليل/إعادة تقديم JSON باستخدام الوسيطة (يع!)
- تفعل ذلك القضبان الطريق!
ثم ...محل ActionController::العارض عن سلمان!فقط قم بإضافة التعليمات البرمجية التالية إلى ApplicationController:
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
تحقق من awesome_print.تحليل سلسلة JSON إلى روبي التجزئة ، ثم عرض مع awesome_print مثل ذلك:
require "awesome_print"
require "json"
json = '{"holy": ["nested", "json"], "batman!": {"a": 1, "b": 2}}'
ap(JSON.parse(json))
مع ما سبق ، سوف ترى:
{
"holy" => [
[0] "nested",
[1] "json"
],
"batman!" => {
"a" => 1,
"b" => 2
}
}
awesome_print أيضا إضافة بعض الألوان التي تجاوز سعة مكدس لن تظهر لك :)
إلقاء أحد ActiveRecord كائن JSON (في القضبان وحدة التحكم):
pp User.first.as_json
# => {
"id" => 1,
"first_name" => "Polar",
"last_name" => "Bear"
}
إذا كنت مثل (أنا) نجد أن pretty_generate
الخيار بنيت في روبي سلمان المكتبة ليست "جميلة" بما فيه الكفاية ، أوصي بلدي NeatJSON
جوهرة من أجل التنسيق الخاص بك.
لاستخدامه gem install neatjson
ومن ثم استخدام JSON.neat_generate
بدلا من JSON.pretty_generate
.
مثل روبي 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?"}
]
}
كما يدعم مجموعة متنوعة من خيارات التنسيق لمزيد من تخصيص الإخراج الخاص بك.على سبيل المثال, كيف العديد من الأماكن قبل/بعد نقطتين?قبل/بعد بالفواصل ؟ داخل الأقواس من المصفوفات و الكائنات ؟ هل ترغب في فرز المفاتيح الخاصة بك ؟ هل تريد نقطتين لجميع تكون مبطنة ؟
باستخدام <pre>
html pretty_generate
هو خدعة جيدة:
<%
require 'json'
hash = JSON[{hey: "test", num: [{one: 1, two: 2, threes: [{three: 3, tthree: 33}]}]}.to_json]
%>
<pre>
<%= JSON.pretty_generate(hash) %>
</pre>
هنا هو حل الوسيطة معدلة من هذا ممتاز الإجابة من قبل @gertas.هذا الحل ليس القضبان محددة--وينبغي أن تعمل مع أي رف التطبيق.
الوسيطة التقنية المستخدمة هنا ، باستخدام #كل, هو موضح في ASCIIcasts 151:رف الوسيطة قبل Eifion بيدفورد.
هذا الرمز يذهب في التطبيق/الوسيطة/pretty_json_response.rb:
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
لتشغيله ، هذا إضافة إلى التكوين/بيئات/اختبار.rb و التكوين/بيئات/التنمية.rb:
config.middleware.use "PrettyJsonResponse"
كما @gertas يحذر في روايته من هذا الحل ، تجنب استخدامه في الإنتاج.انها بطيئة نوعا ما.
اختبار مع القضبان 4.1.6.
#At Controller
def branch
@data = Model.all
render json: JSON.pretty_generate(@data.as_json)
end
هنا الحل بلادي التي أنا المستمدة من الوظائف الأخرى أثناء البحث الخاص.
هذا يسمح لك لإرسال pp و جي جي الإخراج إلى ملف حسب الحاجة.
require "pp"
require "json"
class File
def pp(*objs)
objs.each {|obj|
PP.pp(obj, self)
}
objs.size <= 1 ? objs.first : objs
end
def jj(*objs)
objs.each {|obj|
obj = JSON.parse(obj.to_json)
self.puts JSON.pretty_generate(obj)
}
objs.size <= 1 ? objs.first : objs
end
end
test_object = { :name => { first: "Christopher", last: "Mullins" }, :grades => [ "English" => "B+", "Algebra" => "A+" ] }
test_json_object = JSON.parse(test_object.to_json)
File.open("log/object_dump.txt", "w") do |file|
file.pp(test_object)
end
File.open("log/json_dump.txt", "w") do |file|
file.jj(test_json_object)
end
لقد استخدمت جوهرة CodeRay وأنه يعمل بشكل جيد جدا.شكل تشمل الألوان و يعترف الكثير من الأشكال المختلفة.
لقد استخدمت على الأحجار الكريمة التي يمكن استخدامها من أجل التصحيح القضبان واجهات برمجة التطبيقات و أنه يعمل بشكل جيد جدا.
بالمناسبة, جوهرة يدعى 'api_explorer' (http://www.github.com/toptierlabs/api_explorer)
إذا كنت تبحث بسرعة تنفيذ هذه القضبان تحكم العمل إلى إرسال استجابة JSON:
def index
my_json = '{ "key": "value" }'
render json: JSON.pretty_generate( JSON.parse my_json )
end
يمكنني استخدام التالية كما يمكنني العثور على رؤوس وضع سلمان إخراج مفيدة مجموعة.استدعاء روتين مكسورة على توصية من railscasts عرض في: http://railscasts.com/episodes/151-rack-middleware?autoplay=true
class LogJson
def initialize(app)
@app = app
end
def call(env)
dup._call(env)
end
def _call(env)
@status, @headers, @response = @app.call(env)
[@status, @headers, self]
end
def each(&block)
if @headers["Content-Type"] =~ /^application\/json/
obj = JSON.parse(@response.body)
pretty_str = JSON.pretty_unparse(obj)
@headers["Content-Length"] = Rack::Utils.bytesize(pretty_str).to_s
Rails.logger.info ("HTTP Headers: #{ @headers } ")
Rails.logger.info ("HTTP Status: #{ @status } ")
Rails.logger.info ("JSON Response: #{ pretty_str} ")
end
@response.each(&block)
end
end
إذا كنت تستخدم RABL يمكنك تكوين كما هو موضح هنا استخدام جسون.pretty_generate:
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
مشكلة مع استخدام جسون.pretty_generate أن سلمان مخطط المصادقون لن تكون سعيدة مع التاريخ والوقت السلاسل.يمكنك إصلاح هذه في التكوين/المهيآت/rabl_config.rb مع:
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
# 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
جميلة الطباعة البديل:
my_object = { :array => [1, 2, 3, { :sample => "hash"}, 44455, 677778, 9900 ], :foo => "bar", rrr: {"pid": 63, "state": false}}
puts my_object.as_json.pretty_inspect.gsub('=>', ': ')
النتيجة:
{"array": [1, 2, 3, {"sample": "hash"}, 44455, 677778, 9900],
"foo": "bar",
"rrr": {"pid": 63, "state": false}}