Skip to content
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
15 changes: 6 additions & 9 deletions lib/fluent/plugin/parser_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
31 changes: 31 additions & 0 deletions test/plugin/test_parser_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading