@@ -74,15 +74,6 @@ def generator=(generator) # :nodoc:
74
74
$VERBOSE = old
75
75
end
76
76
77
- def create_pretty_state
78
- State . new (
79
- :indent => ' ' ,
80
- :space => ' ' ,
81
- :object_nl => "\n " ,
82
- :array_nl => "\n "
83
- )
84
- end
85
-
86
77
# Returns the JSON generator module that is used by JSON.
87
78
attr_reader :generator
88
79
@@ -366,6 +357,14 @@ def fast_generate(obj, opts = nil)
366
357
generate ( obj , opts )
367
358
end
368
359
360
+ PRETTY_GENERATE_OPTIONS = {
361
+ indent : ' ' ,
362
+ space : ' ' ,
363
+ object_nl : "\n " ,
364
+ array_nl : "\n " ,
365
+ } . freeze
366
+ private_constant :PRETTY_GENERATE_OPTIONS
367
+
369
368
# :call-seq:
370
369
# JSON.pretty_generate(obj, opts = nil) -> new_string
371
370
#
@@ -397,22 +396,24 @@ def fast_generate(obj, opts = nil)
397
396
# }
398
397
#
399
398
def pretty_generate ( obj , opts = nil )
400
- if State === opts
401
- state , opts = opts , nil
402
- else
403
- state = JSON . create_pretty_state
404
- end
399
+ return state . generate ( obj ) if State === opts
400
+
401
+ options = PRETTY_GENERATE_OPTIONS
402
+
405
403
if opts
406
- if opts . respond_to? :to_hash
407
- opts = opts . to_hash
408
- elsif opts . respond_to? :to_h
409
- opts = opts . to_h
410
- else
411
- raise TypeError , "can't convert #{ opts . class } into Hash"
404
+ unless opts . is_a? ( Hash )
405
+ if opts . respond_to? :to_hash
406
+ opts = opts . to_hash
407
+ elsif opts . respond_to? :to_h
408
+ opts = opts . to_h
409
+ else
410
+ raise TypeError , "can't convert #{ opts . class } into Hash"
411
+ end
412
412
end
413
- state . configure ( opts )
413
+ options = options . merge ( opts )
414
414
end
415
- state . generate ( obj )
415
+
416
+ State . generate ( obj , options , nil )
416
417
end
417
418
418
419
# Sets or returns default options for the JSON.unsafe_load method.
0 commit comments