1818require 'fluent/time'
1919require 'fluent/oj_options'
2020
21- require 'yajl'
2221require 'json'
2322
2423module Fluent
@@ -28,12 +27,10 @@ class JSONParser < Parser
2827
2928 config_set_default :time_key , 'time'
3029 desc 'Set JSON parser'
30+ # NOTE: Contains yajl for backward compatibility
3131 config_param :json_parser , :enum , list : [ :oj , :yajl , :json ] , default : :oj
3232
33- # The Yajl library defines a default buffer size of 8KiB when parsing
34- # from IO streams, so maintain this for backwards-compatibility.
35- # https://www.rubydoc.info/github/brianmario/yajl-ruby/Yajl%2FParser:parse
36- desc 'Set the buffer size that Yajl will use when parsing streaming input'
33+ desc 'Set the buffer size that JSON parser will use when parsing streaming input'
3734 config_param :stream_buffer_size , :integer , default : 8192
3835
3936 config_set_default :time_type , :float
@@ -55,7 +52,6 @@ def configure_json_parser(name)
5552 log &.info "Oj is not installed, and failing back to JSON for json parser"
5653 configure_json_parser ( :json )
5754 when :json then [ JSON . method ( :parse ) , JSON ::ParserError ]
58- when :yajl then [ Yajl . method ( :load ) , Yajl ::ParseError ]
5955 else
6056 raise "BUG: unknown json parser specified: #{ name } "
6157 end
@@ -94,11 +90,15 @@ def parser_type
9490 end
9591
9692 def parse_io ( io , &block )
97- y = Yajl ::Parser . new
98- y . on_parse_complete = -> ( record ) {
99- block . call ( parse_time ( record ) , record )
100- }
101- y . parse ( io , @stream_buffer_size )
93+ parser = JSON ::Ext ::ResumableParser . new ( { } )
94+ while ( chunk = io . read ( @stream_buffer_size ) )
95+ parser << chunk
96+
97+ while parser . parse
98+ record = parser . value
99+ block . call ( parse_time ( record ) , record )
100+ end
101+ end
102102 end
103103 end
104104 end
0 commit comments