Class: Fluent::Plugin::JSONInJSONParser

Inherits:
Parser
  • Object
show all
Defined in:
lib/fluent/plugin/parser_json_in_json.rb

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/fluent/plugin/parser_json_in_json.rb', line 12

def configure(conf)
  if conf.has_key?('time_format')
    conf['time_type'] ||= 'string'
  end

  super
end

#parse(text) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fluent/plugin/parser_json_in_json.rb', line 20

def parse(text)
  record = Yajl.load(text)

  values = Hash.new

  record.each do |k, v|
    if v.is_a?(String) && /^\s*(\{|\[)/ =~ v
      begin
        deserialized = Yajl.load(v)
        if deserialized.is_a?(Hash)
          values.merge!(deserialized)
          record.delete k
        elsif deserialized.is_a?(Array)
          values[k] = deserialized
        end
      rescue Yajl::ParseError
        # continue if failed to parse record
      end
    end
  end
  record.merge!(values)

  time, record = convert_values(parse_time(record), record)

  yield time, record
rescue Yajl::ParseError
  yield nil, nil
end