summaryrefslogtreecommitdiff
path: root/ext/json/lib/json/common.rb
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-03-11 13:36:06 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-03-11 13:36:06 +0000
commit4d8d3184d18b1a9d2eadb86a6688dc62eca7eabc (patch)
tree03e3a00e4e484ffdb46d5d774ab887dc695b8ed0 /ext/json/lib/json/common.rb
parent9d9ab5a384de3877f29cef8c0293c1caa432692c (diff)
* ext/json: Merge 164a75c8bd2007d32c4d7665d53140d8fc126dcd.
[ruby-core:41917] [Bug #5846] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34971 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/json/lib/json/common.rb')
-rw-r--r--ext/json/lib/json/common.rb45
1 files changed, 40 insertions, 5 deletions
diff --git a/ext/json/lib/json/common.rb b/ext/json/lib/json/common.rb
index 43e249c954..cf7a8b9003 100644
--- a/ext/json/lib/json/common.rb
+++ b/ext/json/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