diff options
Diffstat (limited to 'ext/json/lib/json/common.rb')
-rw-r--r-- | ext/json/lib/json/common.rb | 35 |
1 files changed, 16 insertions, 19 deletions
diff --git a/ext/json/lib/json/common.rb b/ext/json/lib/json/common.rb index 244634b7a1..d5444b92c8 100644 --- a/ext/json/lib/json/common.rb +++ b/ext/json/lib/json/common.rb @@ -11,9 +11,9 @@ module JSON # generate and parse for their documentation. def [](object, opts = {}) if object.respond_to? :to_str - JSON.parse(object.to_str, opts => {}) + JSON.parse(object.to_str, opts) else - JSON.generate(object, opts => {}) + JSON.generate(object, opts) end end @@ -40,8 +40,8 @@ module JSON else begin p.const_missing(c) - rescue NameError - raise ArgumentError, "can't find const #{path}" + rescue NameError => e + raise ArgumentError, "can't get const #{path}: #{e}" end end end @@ -49,6 +49,7 @@ module JSON # Set the module _generator_ to be used by JSON. def generator=(generator) # :nodoc: + old, $VERBOSE = $VERBOSE, nil @generator = generator generator_methods = generator::GeneratorMethods for const in generator_methods.constants @@ -63,20 +64,22 @@ module JSON end self.state = generator::State const_set :State, self.state - const_set :SAFE_STATE_PROTOTYPE, State.new.freeze + const_set :SAFE_STATE_PROTOTYPE, State.new const_set :FAST_STATE_PROTOTYPE, State.new( :indent => '', :space => '', :object_nl => "", :array_nl => "", :max_nesting => false - ).freeze + ) const_set :PRETTY_STATE_PROTOTYPE, State.new( :indent => ' ', :space => ' ', :object_nl => "\n", :array_nl => "\n" - ).freeze + ) + ensure + $VERBOSE = old end # Returns the JSON generator modul, that is used by JSON. This might be @@ -196,6 +199,7 @@ module JSON # amount of sanity checks, and the pretty_generate method for some # defaults for a pretty output. def generate(obj, opts = nil) + state = SAFE_STATE_PROTOTYPE.dup if opts if opts.respond_to? :to_hash opts = opts.to_hash @@ -204,10 +208,7 @@ module JSON else raise TypeError, "can't convert #{opts.class} into Hash" end - state = SAFE_STATE_PROTOTYPE.dup state = state.configure(opts) - else - state = SAFE_STATE_PROTOTYPE end state.generate(obj) end @@ -225,6 +226,7 @@ module JSON # *WARNING*: Be careful not to pass any Ruby data structures with circles as # _obj_ argument, because this will cause JSON to go into an infinite loop. def fast_generate(obj, opts = nil) + state = FAST_STATE_PROTOTYPE.dup if opts if opts.respond_to? :to_hash opts = opts.to_hash @@ -233,10 +235,7 @@ module JSON else raise TypeError, "can't convert #{opts.class} into Hash" end - state = FAST_STATE_PROTOTYPE.dup state.configure(opts) - else - state = FAST_STATE_PROTOTYPE end state.generate(obj) end @@ -254,6 +253,7 @@ module JSON # The _opts_ argument can be used to configure the generator, see the # generate method for a more detailed explanation. def pretty_generate(obj, opts = nil) + state = PRETTY_STATE_PROTOTYPE.dup if opts if opts.respond_to? :to_hash opts = opts.to_hash @@ -262,10 +262,7 @@ module JSON else raise TypeError, "can't convert #{opts.class} into Hash" end - state = PRETTY_STATE_PROTOTYPE.dup state.configure(opts) - else - state = PRETTY_STATE_PROTOTYPE end state.generate(obj) end @@ -377,11 +374,11 @@ module ::Kernel # # The _opts_ argument is passed through to generate/parse respectively, see # generate and parse for their documentation. - def JSON(object, opts = {}) + def JSON(object, *args) if object.respond_to? :to_str - JSON.parse(object.to_str, opts) + JSON.parse(object.to_str, args.first) else - JSON.generate(object, opts) + JSON.generate(object, args.first) end end end |