diff --git a/lib/fluent/plugin/parser_json.rb b/lib/fluent/plugin/parser_json.rb index 45fe8607c7..0537145800 100644 --- a/lib/fluent/plugin/parser_json.rb +++ b/lib/fluent/plugin/parser_json.rb @@ -67,16 +67,18 @@ def parse(text) parsed_json = @load_proc.call(text) if parsed_json.is_a?(Hash) - time, record = parse_one_record(parsed_json) - yield time, record + time = parse_time(parsed_json) + time, parsed_json = convert_values(time, parsed_json) if @execute_convert_values + yield time, parsed_json elsif parsed_json.is_a?(Array) parsed_json.each do |record| unless record.is_a?(Hash) yield nil, nil next end - time, parsed_record = parse_one_record(record) - yield time, parsed_record + time = parse_time(record) + time, record = convert_values(time, record) if @execute_convert_values + yield time, record end else yield nil, nil @@ -86,11 +88,6 @@ def parse(text) yield nil, nil end - def parse_one_record(record) - time = parse_time(record) - convert_values(time, record) - end - def parser_type :text end diff --git a/test/plugin/test_parser_json.rb b/test/plugin/test_parser_json.rb index 95516208e8..16f22e802e 100644 --- a/test/plugin/test_parser_json.rb +++ b/test/plugin/test_parser_json.rb @@ -197,6 +197,37 @@ def test_parse_with_keep_time_key_without_time_format(data) end end + sub_test_case "convert_values" do + def test_parse_applies_types + @parser.configure('types' => 'size:string') + results = [] + @parser.instance.parse('{"time":1362020400,"size":777}') { |time, record| + results << [time.to_i, record] + } + assert_equal([[event_time('2013-02-28 12:00:00 +0900').to_i, {'size' => '777'}]], results) + end + + data('no conversion' => [{}, 0], + 'types' => [{'types' => 'k:string'}, 2], + 'null_empty_string' => [{'null_empty_string' => 'true'}, 2], + 'null_value_pattern' => [{'null_value_pattern' => '^-$'}, 2]) + def test_parse_calls_convert_values_only_with_conversion((conf, expected)) + klass = Class.new(Fluent::Plugin::JSONParser) do + attr_reader :convert_count + + def convert_values(time, record) + @convert_count = @convert_count.to_i + 1 + super + end + end + parser = Fluent::Test::Driver::Parser.new(klass) + parser.configure(conf) + parser.instance.parse('{"k":"v"}') { |time, record| } + parser.instance.parse('[{"k":"v"}]') { |time, record| } + assert_equal(expected, parser.instance.convert_count.to_i) + end + end + def test_yajl_parse_io_with_buffer_smaller_than_input parser = Fluent::Test::Driver::Parser.new(Fluent::Plugin::JSONParser) parser.configure(