Class: Fluent::TextParser::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



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

def configure(conf)
  super

  unless @time_format.nil?
    @time_parser = TimeParser.new(@time_format)
    @mutex = Mutex.new
  end
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fluent/plugin/parser_json_in_json.rb', line 20

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

  value = @keep_time_key ? record[@time_key] : record.delete(@time_key)
  if value
    if @time_format
      time = @mutex.synchronize { @time_parser.parse(value) }
    else
      begin
        time = value.to_i
      rescue => e
        raise ParserError, "invalid time value: value = #{value}, error_class = #{e.class.name}, error = #{e.message}"
      end
    end
  else
    if @estimate_current_event
      time = Engine.now
    else
      time = nil
    end
  end

  values = Hash.new
  record.each do |k, v|
    if v[0] == '{'
      deserialized = Yajl.load(v)
      if deserialized.is_a?(Hash)
        values.merge!(deserialized)
        record.delete k
      end
    end
  end
  record.merge!(values)

  if block_given?
    yield time, record
  else
    return time, record
  end
rescue Yajl::ParseError
  if block_given?
    yield nil, nil
  else
    return nil, nil
  end
end