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
# 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) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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) ⇒ Object



96
97
98
99
100
101
102
# 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) ⇒ Object



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

def parse_one_record(record)
  time = parse_time(record)
  convert_values(time, record)
end

#parser_typeObject



92
93
94
# File 'lib/fluent/plugin/parser_json.rb', line 92

def parser_type
  :text
end