Skip to content

Commit

Permalink
Serialize Fluent::EventTime as Integer in JSON. fixes #897
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuki Ito committed Apr 20, 2016
1 parent 1a3df5e commit d5ae120
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 d5ae120

Please sign in to comment.