Skip to content

Commit

Permalink
Merge pull request #904 from mururu/event-time-json
Browse files Browse the repository at this point in the history
Serialize Fluent::EventTime as Integer in JSON. fixes #897
  • Loading branch information
tagomoris committed Apr 21, 2016
2 parents ccb2046 + d5ae120 commit 87b39e8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/fluent/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ def to_s
@sec.to_s
end

def to_json(*args)
@sec.to_s
end

def to_msgpack(io = nil)
@sec.to_msgpack(io)
end
Expand Down
30 changes: 30 additions & 0 deletions test/test_event_time.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require_relative 'helper'
require 'timecop'
require 'oj'
require 'yajl'

class EventTimeTest < Test::Unit::TestCase
setup do
Expand Down Expand Up @@ -34,6 +36,34 @@ class EventTimeTest < Test::Unit::TestCase
assert_equal('100', "#{time}")
end

test '#to_json' do
time = Fluent::EventTime.new(100)
assert_equal('100', time.to_json)
assert_equal('{"time":100}', {'time' => time}.to_json)
assert_equal('["tag",100,{"key":"value"}]', ["tag", time, {"key" => "value"}].to_json)
end

test 'JSON.dump' do
time = Fluent::EventTime.new(100)
assert_equal('100', JSON.dump(time))
assert_equal('{"time":100}', JSON.dump({'time' => time}))
assert_equal('["tag",100,{"key":"value"}]', JSON.dump(["tag", time, {"key" => "value"}]))
end

test 'Oj.dump' do
time = Fluent::EventTime.new(100)
assert_equal('100', Oj.dump(time, mode: :compat))
assert_equal('{"time":100}', Oj.dump({'time' => time}, mode: :compat))
assert_equal('["tag",100,{"key":"value"}]', Oj.dump(["tag", time, {"key" => "value"}], mode: :compat))
end

test 'Yajl.dump' do
time = Fluent::EventTime.new(100)
assert_equal('100', Yajl.dump(time))
assert_equal('{"time":100}', Yajl.dump({'time' => time}))
assert_equal('["tag",100,{"key":"value"}]', Yajl.dump(["tag", time, {"key" => "value"}]))
end

test '.from_time' do
sec = 1000
usec = 2
Expand Down

0 comments on commit 87b39e8

Please sign in to comment.