add a way to ask for pretty print json for the webapi

Fixes #4658

Fixes #4653
This commit is contained in:
Pere Urbon-Bayes 2016-02-09 15:21:47 +01:00 committed by Pier-Hugues Pellerin
parent 65ee298280
commit 2c4b8e8fd5
4 changed files with 22 additions and 7 deletions

View file

@ -15,8 +15,8 @@ module LogStash::Api
:human => params.has_key?("human")
}
command = factory.build(:hot_threads_command)
type = options[:human] ? :string : :json
respond_with(command.run(options), type)
as = options[:human] ? :string : :json
respond_with(command.run(options), {:as => as})
end
end

View file

@ -3,10 +3,12 @@ require "logstash/json"
module LogStash::Api::AppHelpers
def respond_with(data, as=:json)
def respond_with(data, options={})
as = options.fetch(:as, :json)
pretty = params.has_key?("pretty")
if as == :json
content_type "application/json"
LogStash::Json.dump(data)
LogStash::Json.dump(data, {:pretty => pretty})
else
content_type "text/plain"
data.to_s

View file

@ -41,13 +41,12 @@ module LogStash
raise LogStash::Json::ParserError.new(e.message)
end
def jruby_dump(o)
def jruby_dump(o, options={})
# TODO [guyboertje] remove these comments in 5.0
# test for enumerable here to work around an omission in JrJackson::Json.dump to
# also look for Java::JavaUtil::ArrayList, see TODO submit issue
# o.is_a?(Enumerable) ? JrJackson::Raw.generate(o) : JrJackson::Json.dump(o)
JrJackson::Base.generate(o, {})
JrJackson::Base.generate(o, options)
rescue => e
raise LogStash::Json::GeneratorError.new(e.message)

View file

@ -59,6 +59,20 @@ describe "LogStash::Json" do
expect(LogStash::Json.dump(array)).to eql(json_array)
end
context "pretty print" do
let(:hash) { { "foo" => "bar", :zoo => 2 } }
it "should serialize with pretty print" do
pprint_json = LogStash::Json.dump(hash, :pretty => true)
expect(pprint_json).to include("\n")
end
it "should by default do no pretty print" do
pprint_json = LogStash::Json.dump(hash)
expect(pprint_json).not_to include("\n")
end
end
end
else