class Fluent::TextParser::JSONParser

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method Fluent::Parser#configure
# File lib/fluent/parser.rb, line 252
def configure(conf)
  super

  unless @time_format.nil?
    @time_parser = TimeParser.new(@time_format)
    @mutex = Mutex.new
  end

  begin
    raise LoadError unless @json_parser == 'oj'
    require 'oj'
    Oj.default_options = {bigdecimal_load: :float}
    @load_proc = Oj.method(:load)
    @error_class = Oj::ParseError
  rescue LoadError
    @load_proc = Yajl.method(:load)
    @error_class = Yajl::ParseError
  end
end
parse(text) { |time, record| ... } click to toggle source
# File lib/fluent/parser.rb, line 272
def parse(text)
  record = @load_proc.call(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

  if block_given?
    yield time, record
  else
    return time, record
  end
rescue @error_class
  if block_given?
    yield nil, nil
  else
    return nil, nil
  end
end