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?, #initialize, #parse_partial_data, #parse_time, #parse_with_timeout, #start, #stop, #string_like_null

Methods included from TimeMixin::Parser

included, #time_parser_create

Methods included from OwnedByMixin

#log, #owner, #owner=

Methods inherited from Base

#acquire_worker_lock, #after_shutdown, #after_shutdown?, #after_start, #after_started?, #before_shutdown, #before_shutdown?, #called_in_test?, #close, #closed?, #configured?, #context_router, #context_router=, #fluentd_worker_id, #get_lock_path, #has_router?, #initialize, #inspect, #multi_workers_ready?, #plugin_root_dir, #reloadable_plugin?, #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::Parser

Instance Method Details

#configure(conf) ⇒ Object



41
42
43
44
45
46
47
48
# 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) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/fluent/plugin/parser_json.rb', line 50

def configure_json_parser(name)
  case name
  when :oj
    raise LoadError unless Fluent::OjOptions.available?
    [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 => ex
  name = :yajl
  if log
    if /\boj\z/.match?(ex.message)
      log.info "Oj is not installed, and failing back to Yajl for json parser"
    else
      log.warn ex.message
    end
  end
  retry
end

#parse(text) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/fluent/plugin/parser_json.rb', line 72

def parse(text)
  record = @load_proc.call(text)
  time = parse_time(record)
  if @execute_convert_values
    time, record = convert_values(time, record)
  end
  yield time, record
rescue @error_class, EncodingError # EncodingError is for oj 3.x or later
  yield nil, nil
end

#parse_io(io, &block) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/fluent/plugin/parser_json.rb', line 87

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

#parser_typeObject



83
84
85
# File 'lib/fluent/plugin/parser_json.rb', line 83

def parser_type
  :text
end