Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use compat instead of strict to follow json/yajl way. fix #1230 #1239

Merged
merged 2 commits into from
Sep 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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