diff options
author | Florian Frank <flori@ping.de> | 2011-12-21 11:05:46 +0100 |
---|---|---|
committer | Florian Frank <flori@ping.de> | 2011-12-21 11:05:46 +0100 |
commit | 60ef7adb347b473cbcce9aacacb2943ae5fb4d4c (patch) | |
tree | ba73bea0660d74b2a9a787c5e0acd1a06739e1b6 /lib/json/common.rb | |
parent | 0dacb54bcdf3c40cc38dae26f04b780024460b45 (diff) | |
parent | 59ecfad89281873fe72234b62545294b5fa7ba95 (diff) | |
download | json-MagLev-master.tar.gz |
Merge branch 'master' of https://github.com/MagLev/json into MagLev-masterMagLev-master
Diffstat (limited to 'lib/json/common.rb')
-rw-r--r-- | lib/json/common.rb | 45 |
1 files changed, 40 insertions, 5 deletions
diff --git a/lib/json/common.rb b/lib/json/common.rb index 43e249c..cf7a8b9 100644 --- a/lib/json/common.rb +++ b/lib/json/common.rb @@ -284,22 +284,40 @@ module JSON module_function :pretty_unparse # :startdoc: + class << self + # The global default options for the JSON.load method: + # :max_nesting: false + # :allow_nan: true + # :quirks_mode: true + attr_accessor :load_default_options + end + self.load_default_options = { + :max_nesting => false, + :allow_nan => true, + :quirks_mode => true, + } + # Load a ruby data structure from a JSON _source_ and return it. A source can # either be a string-like object, an IO-like object, or an object responding # to the read method. If _proc_ was given, it will be called with any nested - # Ruby object as an argument recursively in depth first order. + # Ruby object as an argument recursively in depth first order. The default + # options for the parser can be changed via the load_default_options method. # # This method is part of the implementation of the load/dump interface of # Marshal and YAML. def load(source, proc = nil) + opts = load_default_options if source.respond_to? :to_str source = source.to_str elsif source.respond_to? :to_io source = source.to_io.read - else + elsif source.respond_to?(:read) source = source.read end - result = parse(source, :max_nesting => false, :allow_nan => true) + if opts[:quirks_mode] && (source.nil? || source.empty?) + source = 'null' + end + result = parse(source, opts) recurse_proc(result, &proc) if proc result end @@ -321,6 +339,19 @@ module JSON alias restore load module_function :restore + class << self + # The global default options for the JSON.dump method: + # :max_nesting: false + # :allow_nan: true + # :quirks_mode: true + attr_accessor :dump_default_options + end + self.dump_default_options = { + :max_nesting => false, + :allow_nan => true, + :quirks_mode => true, + } + # Dumps _obj_ as a JSON string, i.e. calls generate on the object and returns # the result. # @@ -331,6 +362,9 @@ module JSON # exception is raised. This argument is similar (but not exactly the # same!) to the _limit_ argument in Marshal.dump. # + # The default options for the generator can be changed via the + # dump_default_options method. + # # This method is part of the implementation of the load/dump interface of # Marshal and YAML. def dump(obj, anIO = nil, limit = nil) @@ -341,8 +375,9 @@ module JSON anIO = nil end end - limit ||= 0 - result = generate(obj, :allow_nan => true, :max_nesting => limit) + opts = JSON.dump_default_options + limit and opts.update(:max_nesting => limit) + result = generate(obj, opts) if anIO anIO.write result anIO |