Class: Fluent::Plugin::JSONParser
- Defined in:
- lib/fluent/plugin/parser_json.rb
Direct Known Subclasses
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
Constants included from Configurable
Configurable::CONFIG_TYPE_REGISTRY
Instance Attribute Summary
Attributes inherited from Parser
Attributes inherited from Base
Instance Method Summary collapse
- #configure(conf) ⇒ Object
- #configure_json_parser(name) ⇒ Object
- #parse(text) ⇒ Object
- #parse_io(io, &block) ⇒ Object
- #parse_one_record(record) ⇒ Object
- #parser_type ⇒ Object
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
Methods included from OwnedByMixin
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
43 44 45 46 47 48 49 50 |
# File 'lib/fluent/plugin/parser_json.rb', line 43 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
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/fluent/plugin/parser_json.rb', line 52 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 :yajl, :json # NOTE: Fallback yajl to json for backward compatibility [JSON_PARSE_PROC, JSON::ParserError] else raise "BUG: unknown json parser specified: #{name}" end end |
#parse(text) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/fluent/plugin/parser_json.rb', line 66 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
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/fluent/plugin/parser_json.rb', line 98 def parse_io(io, &block) parser = JSON::ResumableParser.new(**Fluent::DEFAULT_JSON_PARSE_OPTIONS) begin chunk = +"".b while io.readpartial(@stream_buffer_size, chunk) parser << chunk while parser.parse record = parser.value block.call(parse_time(record), record) end end rescue EOFError unless parser.empty? log&.warn "JSON stream ended in the middle of a document; " \ "discarding incomplete data" end end end |
#parse_one_record(record) ⇒ Object
89 90 91 92 |
# File 'lib/fluent/plugin/parser_json.rb', line 89 def parse_one_record(record) time = parse_time(record) convert_values(time, record) end |
#parser_type ⇒ Object
94 95 96 |
# File 'lib/fluent/plugin/parser_json.rb', line 94 def parser_type :text end |