Skip to content

Commit

Permalink
Merge pull request #1239 from fluent/fix-oj-mode
Browse files Browse the repository at this point in the history
Use compat instead of strict to follow json/yajl way. fix #1230
  • Loading branch information
repeatedly authored Sep 26, 2016
2 parents 08c74bc + d08c38d commit 479257d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/fluent/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module Fluent
DEFAULT_CONFIG_PATH = ENV['FLUENT_CONF'] || '/etc/fluent/fluent.conf'
DEFAULT_PLUGIN_DIR = ENV['FLUENT_PLUGIN'] || '/etc/fluent/plugin'
DEFAULT_SOCKET_PATH = ENV['FLUENT_SOCKET'] || '/var/run/fluent/fluent.sock'
DEFAULT_OJ_OPTIONS = {bigdecimal_load: :float, mode: :compat}
IS_WINDOWS = /mswin|mingw/ === RUBY_PLATFORM
private_constant :IS_WINDOWS

Expand Down
3 changes: 2 additions & 1 deletion lib/fluent/plugin/formatter_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#

require 'fluent/plugin/formatter'
require 'fluent/env'

module Fluent
module Plugin
Expand All @@ -29,7 +30,7 @@ def configure(conf)
begin
raise LoadError unless @json_parser == 'oj'
require 'oj'
Oj.default_options = {mode: :compat}
Oj.default_options = Fluent::DEFAULT_OJ_OPTIONS
@dump_proc = Oj.method(:dump)
rescue LoadError
@dump_proc = Yajl.method(:dump)
Expand Down
3 changes: 2 additions & 1 deletion lib/fluent/plugin/parser_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#

require 'fluent/plugin/parser'
require 'fluent/env'
require 'fluent/time'

require 'yajl'
Expand All @@ -38,7 +39,7 @@ def configure(conf)
begin
raise LoadError unless @json_parser == 'oj'
require 'oj'
Oj.default_options = {bigdecimal_load: :float, mode: :strict}
Oj.default_options = Fluent::DEFAULT_OJ_OPTIONS
@load_proc = Oj.method(:load)
@error_class = Oj::ParseError
rescue LoadError
Expand Down
15 changes: 14 additions & 1 deletion test/plugin/test_formatter_json.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative '../helper'
require 'json'
require 'fluent/test/driver/formatter'
require 'fluent/plugin/formatter_json'

Expand All @@ -20,11 +21,23 @@ def record
{'message' => 'awesome'}
end

def symbolic_record
{:message => :awesome}
end

data('oj' => 'oj', 'yajl' => 'yajl')
def test_format(data)
d = create_driver('json_parser' => data)
formatted = d.instance.format(tag, @time, record)

assert_equal("#{Yajl.dump(record)}\n", formatted)
assert_equal("#{JSON.generate(record)}\n", formatted)
end

data('oj' => 'oj', 'yajl' => 'yajl')
def test_format_with_symbolic_record(data)
d = create_driver('json_parser' => data)
formatted = d.instance.format(tag, @time, symbolic_record)

assert_equal("#{JSON.generate(record)}\n", formatted)
end
end
8 changes: 8 additions & 0 deletions test/plugin/test_parser_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ def test_parse_without_time(data)
}
end

data('oj' => 'oj', 'yajl' => 'yajl')
def test_parse_with_colon_string(data)
@parser.configure('json_parser' => data)
@parser.instance.parse('{"time":1362020400,"log":":message"}') { |time, record|
assert_equal(record['log'], ':message')
}
end

data('oj' => 'oj', 'yajl' => 'yajl')
def test_parse_with_invalid_time(data)
@parser.configure('json_parser' => data)
Expand Down

0 comments on commit 479257d

Please sign in to comment.