Class: Fluent::Plugin::JSONParser

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

Direct Known Subclasses

Compat::TextParser::JSONParser

Constant Summary collapse

JSON_PARSE_PROC =

Use a shared proc rather than a per-call lambda so that configure_json_parser returns the same object every time.

->(text) { JSON.parse(text, **Fluent::DEFAULT_JSON_PARSE_OPTIONS) }

Constants inherited from Parser

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

Constants inherited from Base

Base::LOCK_FILE_BUCKETS

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



46
47
48
49
50
51
52
53
# File 'lib/fluent/plugin/parser_json.rb', line 46

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



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fluent/plugin/parser_json.rb', line 55

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 JSON for json parser"
    configure_json_parser(:json)
  when :json then [JSON_PARSE_PROC, JSON::ParserError]
  when :yajl then [Yajl.method(:load), Yajl::ParseError]
  else
    raise "BUG: unknown json parser specified: #{name}"
  end
end

#parse(text) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fluent/plugin/parser_json.rb', line 69

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



101
102
103
104
105
106
107
# File 'lib/fluent/plugin/parser_json.rb', line 101

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



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

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

#parser_type ⇒ Object



97
98
99
# File 'lib/fluent/plugin/parser_json.rb', line 97

def parser_type
  :text
end