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
@@ -54,8 +51,8 @@ def configure_json_parser(name)
5451
5552 log &.info "Oj is not installed, and failing back to JSON for json parser"
5653 configure_json_parser ( :json )
57- when :json then [ JSON . method ( :parse ) , JSON :: ParserError ]
58- when :yajl then [ Yajl . method ( :load ) , Yajl :: ParseError ]
54+ when :yajl , : json # NOTE: Fallback yajl to json for backward compatibility
55+ [ JSON . method ( :parse ) , JSON :: ParserError ]
5956 else
6057 raise "BUG: unknown json parser specified: #{ name } "
6158 end
@@ -94,11 +91,15 @@ def parser_type
9491 end
9592
9693 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 )
94+ parser = JSON ::Ext ::ResumableParser . new ( { } )
95+ while ( chunk = io . read ( @stream_buffer_size ) )
96+ parser << chunk
97+
98+ while parser . parse
99+ record = parser . value
100+ block . call ( parse_time ( record ) , record )
101+ end
102+ end
102103 end
103104 end
104105 end
0 commit comments