Skip to content

Commit

Permalink
honor common to_json method signature
Browse files Browse the repository at this point in the history
  • Loading branch information
colinsurprenant authored and jordansissel committed Sep 27, 2014
1 parent d2f6118 commit e4ac936
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/logstash/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ def fields
end

public
def to_json
def to_json(*args)
# ignore arguments to respect accepted to_json method signature
LogStash::Json.dump(@data)
end # def to_json

Expand Down
3 changes: 2 additions & 1 deletion lib/logstash/timestamp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ def utc
end
alias_method :gmtime, :utc

def to_json
def to_json(*args)
# ignore arguments to respect accepted to_json method signature
LogStash::Json.dump(@time.iso8601(ISO8601_PRECISION))
end
alias_method :inspect, :to_json
Expand Down
23 changes: 23 additions & 0 deletions spec/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,27 @@
insist{event[LogStash::Event::TIMESTAMP_FAILURE_FIELD]} == "foo"
end
end

context "to_json" do
it "should support to_json" do
new_event = LogStash::Event.new(
"@timestamp" => Time.iso8601("2014-09-23T19:26:15.832Z"),
"message" => "foo bar",
)
json = new_event.to_json

insist { json } == "{\"@timestamp\":\"2014-09-23T19:26:15.832Z\",\"message\":\"foo bar\",\"@version\":\"1\"}"
end

it "should support to_json and ignore arguments" do
new_event = LogStash::Event.new(
"@timestamp" => Time.iso8601("2014-09-23T19:26:15.832Z"),
"message" => "foo bar",
)
json = new_event.to_json(:foo => 1, :bar => "baz")

insist { json } == "{\"@timestamp\":\"2014-09-23T19:26:15.832Z\",\"message\":\"foo bar\",\"@version\":\"1\"}"
end
end

end
7 changes: 7 additions & 0 deletions spec/timestamp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,11 @@
expect(LogStash::Timestamp.coerce(:foobar)).to be_nil
end

it "should support to_json" do
expect(LogStash::Timestamp.parse_iso8601("2014-09-23T00:00:00-0800").to_json).to eq("\"2014-09-23T08:00:00.000Z\"")
end

it "should support to_json and ignore arguments" do
expect(LogStash::Timestamp.parse_iso8601("2014-09-23T00:00:00-0800").to_json(:some => 1, :argumnents => "test")).to eq("\"2014-09-23T08:00:00.000Z\"")
end
end

0 comments on commit e4ac936

Please sign in to comment.