@@ -307,20 +307,18 @@ def generate(obj)
307
307
308
308
# Handles @allow_nan, @buffer_initial_length, other ivars must be the default value (see above)
309
309
private def generate_json ( obj , buf )
310
- klass = obj . class
311
- if klass == Hash
310
+ case obj
311
+ when Hash
312
312
buf << '{'
313
313
first = true
314
314
obj . each_pair do |k , v |
315
315
buf << ',' unless first
316
316
317
317
key_str = k . to_s
318
- if key_str . is_a? ( ::String )
319
- if key_str . class == ::String
320
- fast_serialize_string ( key_str , buf )
321
- else
322
- generate_json ( key_str , buf )
323
- end
318
+ if key_str . class == String
319
+ fast_serialize_string ( key_str , buf )
320
+ elsif key_str . is_a? ( String )
321
+ generate_json ( key_str , buf )
324
322
else
325
323
raise TypeError , "#{ k . class } #to_s returns an instance of #{ key_str . class } , expected a String"
326
324
end
@@ -330,7 +328,7 @@ def generate(obj)
330
328
first = false
331
329
end
332
330
buf << '}'
333
- elsif klass == Array
331
+ when Array
334
332
buf << '['
335
333
first = true
336
334
obj . each do |e |
@@ -339,9 +337,13 @@ def generate(obj)
339
337
first = false
340
338
end
341
339
buf << ']'
342
- elsif klass == String
343
- fast_serialize_string ( obj , buf )
344
- elsif klass == Integer
340
+ when String
341
+ if obj . class == String
342
+ fast_serialize_string ( obj , buf )
343
+ else
344
+ buf << obj . to_json ( self )
345
+ end
346
+ when Integer
345
347
buf << obj . to_s
346
348
else
347
349
# Note: Float is handled this way since Float#to_s is slow anyway
@@ -432,8 +434,8 @@ def json_transform(state)
432
434
result << state . indent * depth if indent
433
435
434
436
key_str = key . to_s
435
- key_json = if key_str . is_a? ( :: String )
436
- key_str = key_str . to_json ( state )
437
+ if key_str . is_a? ( String )
438
+ key_json = key_str . to_json ( state )
437
439
else
438
440
raise TypeError , "#{ key . class } #to_s returns an instance of #{ key_str . class } , expected a String"
439
441
end
0 commit comments