Class: Fluent::Plugin::JSONParser

Inherits:
Parser show all
Defined in:
lib/fluent/plugin/parser_json.rb

Direct Known Subclasses

Compat::TextParser::JSONParser

Constant Summary

Constants inherited from Parser

Parser::AVAILABLE_PARSER_VALUE_TYPES, Parser::PARSER_TYPES, Parser::TRUTHY_VALUES

Constants included from Configurable

Configurable::CONFIG_TYPE_REGISTRY

Instance Attribute Summary

Attributes inherited from Parser

#type_converters

Attributes inherited from Base

#under_plugin_development

Instance Method Summary collapse

Methods inherited from Parser

#build_type_converters, #call, #convert_values, #implement?, #parse_partial_data, #parse_time, #string_like_null

Methods included from TimeMixin::Parser

included, #time_parser_create

Methods included from OwnedByMixin

#log, #owner, #owner=

Methods inherited from Base

#after_shutdown, #after_shutdown?, #after_start, #after_started?, #before_shutdown, #before_shutdown?, #close, #closed?, #configured?, #context_router, #context_router=, #fluentd_worker_id, #has_router?, #initialize, #inspect, #multi_workers_ready?, #plugin_root_dir, #shutdown, #shutdown?, #start, #started?, #stop, #stopped?, #string_safe_encoding, #terminate, #terminated?

Methods included from SystemConfig::Mixin

#system_config, #system_config_override

Methods included from Configurable

#config, #configure_proxy_generate, #configured_section_create, included, #initialize, lookup_type, register_type

Constructor Details

This class inherits a constructor from Fluent::Plugin::Base

Instance Method Details

#configure(conf) ⇒ Object



34
35
36
37
38
39
40
41
# 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) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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) ⇒ Object



60
61
62
63
64
65
66
# 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) ⇒ Object



72
73
74
75
76
77
78
# 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_typeObject



68
69
70
# File 'lib/fluent/plugin/parser_json.rb', line 68

def parser_type
  :text
end