@@ -237,6 +237,8 @@ def configure(opts)
237
237
opts . each do |key , value |
238
238
instance_variable_set "@#{ key } " , value
239
239
end
240
+
241
+ # NOTE: If adding new instance variables here, check whether #generate should check them for #generate_json
240
242
@indent = opts [ :indent ] if opts . key? ( :indent )
241
243
@space = opts [ :space ] if opts . key? ( :space )
242
244
@space_before = opts [ :space_before ] if opts . key? ( :space_before )
@@ -286,12 +288,70 @@ def to_h
286
288
# created this method raises a
287
289
# GeneratorError exception.
288
290
def generate ( obj )
289
- result = obj . to_json ( self )
291
+ if @indent . empty? and @space . empty? and @space_before . empty? and @object_nl . empty? and @array_nl . empty? and
292
+ !@ascii_only and !@script_safe and @max_nesting == 0 and !@strict
293
+ result = generate_json ( obj , '' )
294
+ else
295
+ result = obj . to_json ( self )
296
+ end
290
297
JSON . valid_utf8? ( result ) or raise GeneratorError ,
291
298
"source sequence #{ result . inspect } is illegal/malformed utf-8"
292
299
result
293
300
end
294
301
302
+ # Handles @allow_nan, @buffer_initial_length, other ivars must be the default value (see above)
303
+ private def generate_json ( obj , buf )
304
+ case obj
305
+ when Hash
306
+ buf << '{' . freeze
307
+ first = true
308
+ obj . each_pair do |k , v |
309
+ buf << ',' . freeze unless first
310
+ fast_serialize_string ( k . to_s , buf )
311
+ buf << ':' . freeze
312
+ generate_json ( v , buf )
313
+ first = false
314
+ end
315
+ buf << '}' . freeze
316
+ when Array
317
+ buf << '[' . freeze
318
+ first = true
319
+ obj . each do |e |
320
+ buf << ',' . freeze unless first
321
+ generate_json ( e , buf )
322
+ first = false
323
+ end
324
+ buf << ']' . freeze
325
+ when String
326
+ fast_serialize_string ( obj , buf )
327
+ when Integer
328
+ buf << obj . to_s
329
+ else
330
+ # Note: Float is handled this way since it is Float#to_s is slow anyway
331
+ buf << obj . to_json ( self )
332
+ end
333
+ end
334
+
335
+ # Assumes !@ascii_only, !@script_safe
336
+ if Regexp . method_defined? ( :match )
337
+ private def fast_serialize_string ( string , buf ) # :nodoc:
338
+ buf << '"' . freeze
339
+ string = string . encode ( ::Encoding ::UTF_8 ) unless string . encoding == ::Encoding ::UTF_8
340
+
341
+ if /["\\ \x0 -\x1f ]/n . match? ( string )
342
+ buf << string . gsub ( /["\\ \x0 -\x1f ]/n , MAP )
343
+ else
344
+ buf << string
345
+ end
346
+ buf << '"' . freeze
347
+ end
348
+ else
349
+ # Ruby 2.3 compatibility
350
+ private def fast_serialize_string ( string , buf ) # :nodoc:
351
+ buf << string . to_json ( self )
352
+ end
353
+ end
354
+
295
355
# Return the value returned by method +name+.
296
356
def []( name )
297
357
if respond_to? ( name )
0 commit comments