class Fluent::Plugin::JSONParser

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method Fluent::Plugin::Parser#configure
# File lib/fluent/plugin/parser_json.rb, line 41
def configure(conf)
  if conf.has_key?('time_format')
    conf['time_type'] ||= 'string'
  end

  super
  @load_proc, @error_class = configure_json_parser(@json_parser)
end
configure_json_parser(name) click to toggle source
# File lib/fluent/plugin/parser_json.rb, line 50
def configure_json_parser(name)
  case name
  when :oj
    return [Oj.method(:load), Oj::ParseError] if Fluent::OjOptions.available?

    log&.info "Oj is not installed, and failing back to Yajl for json parser"
    configure_json_parser(:yajl)
  when :json then [JSON.method(:load), JSON::ParserError]
  when :yajl then [Yajl.method(:load), Yajl::ParseError]
  else
    raise "BUG: unknown json parser specified: #{name}"
  end
end
parse(text) { |time, record| ... } click to toggle source
# File lib/fluent/plugin/parser_json.rb, line 64
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
  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
    end
  else
    yield nil, nil
  end

rescue @error_class, EncodingError # EncodingError is for oj 3.x or later
  yield nil, nil
end
parse_io(io, &block) click to toggle source
# File lib/fluent/plugin/parser_json.rb, line 96
def parse_io(io, &block)
  y = Yajl::Parser.new
  y.on_parse_complete = ->(record){
    block.call(parse_time(record), record)
  }
  y.parse(io, @stream_buffer_size)
end
parse_one_record(record) click to toggle source
# File lib/fluent/plugin/parser_json.rb, line 87
def parse_one_record(record)
  time = parse_time(record)
  convert_values(time, record)
end
parser_type() click to toggle source
# File lib/fluent/plugin/parser_json.rb, line 92
def parser_type
  :text
end