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 34
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 43
def configure_json_parser(name)
  case name
  when :oj
    require 'oj'
    Oj.default_options = Fluent::DEFAULT_OJ_OPTIONS
    [Oj.method(:load), Oj::ParseError]
  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
rescue LoadError
  name = :yajl
  log.info "Oj is not installed, and failing back to Yajl for json parser" if log
  retry
end
parse(text) { |time, record| ... } click to toggle source
# File lib/fluent/plugin/parser_json.rb, line 60
def parse(text)
  r = @load_proc.call(text)
  time, record = convert_values(parse_time(r), r)
  yield time, record
rescue @error_class
  yield nil, nil
end
parse_io(io, &block) click to toggle source
# File lib/fluent/plugin/parser_json.rb, line 72
def parse_io(io, &block)
  y = Yajl::Parser.new
  y.on_parse_complete = ->(record){
    block.call(parse_time(record), record)
  }
  y.parse(io)
end
parser_type() click to toggle source
# File lib/fluent/plugin/parser_json.rb, line 68
def parser_type
  :text
end