summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Frank <flori@ping.de>2011-11-22 10:19:59 +0100
committerFlorian Frank <flori@ping.de>2011-11-22 10:19:59 +0100
commit8828e9cd225ac0591b3a5015a53cb91b7232bd83 (patch)
tree08de0fd65287dabe6971128ecd32c4c71163409d
parent95e76412ae545357727420bf06e2e4744ce830fa (diff)
downloadjson-8828e9cd225ac0591b3a5015a53cb91b7232bd83.tar.gz
Make JSON.dump and JSON.load support Rails better
-rw-r--r--CHANGES2
-rw-r--r--lib/json/common.rb42
-rwxr-xr-xtests/test_json.rb15
-rwxr-xr-xtests/test_json_generate.rb2
4 files changed, 56 insertions, 5 deletions
diff --git a/CHANGES b/CHANGES
index 314de04..d78ba98 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,8 @@
2011-11-21 (1.6.2)
* Add support for OpenStruct and BigDecimal.
* Fix bug when parsing nil in quirks_mode.
+ * Make JSON.dump and JSON.load methods better cooperate with Rails' serialize
+ method. Just use: serialize :value, JSON
2011-09-18 (1.6.1)
* Using -target 1.5 to force Java bits to compile with 1.5.
2011-09-12 (1.6.0)
diff --git a/lib/json/common.rb b/lib/json/common.rb
index e0d55a0..fcb1f82 100644
--- a/lib/json/common.rb
+++ b/lib/json/common.rb
@@ -284,22 +284,39 @@ 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
elsif source.respond_to?(:read)
source = source.read
+ elsif source.nil? && opts[:quirks_mode]
+ source = 'null'
end
- result = parse(source, :max_nesting => false, :allow_nan => true)
+ result = parse(source, opts)
recurse_proc(result, &proc) if proc
result
end
@@ -321,6 +338,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 +361,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 +374,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
diff --git a/tests/test_json.rb b/tests/test_json.rb
index 5492b8e..40774b3 100755
--- a/tests/test_json.rb
+++ b/tests/test_json.rb
@@ -4,6 +4,7 @@
require 'test/unit'
require File.join(File.dirname(__FILE__), 'setup_variant')
require 'stringio'
+require 'tempfile'
unless Array.method_defined?(:permutation)
begin
@@ -416,7 +417,19 @@ EOT
JSON.parse('{"foo":"bar", "baz":"quux"}', :symbolize_names => true))
end
- def test_load_dump
+ def test_load
+ assert_equal @hash, JSON.load(@json)
+ tempfile = Tempfile.open('json')
+ tempfile.write @json
+ tempfile.rewind
+ assert_equal @hash, JSON.load(tempfile)
+ stringio = StringIO.new(@json)
+ stringio.rewind
+ assert_equal @hash, JSON.load(stringio)
+ assert_equal nil, JSON.load(nil)
+ end
+
+ def test_dump
too_deep = '[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]'
assert_equal too_deep, JSON.dump(eval(too_deep))
assert_kind_of String, Marshal.dump(eval(too_deep))
diff --git a/tests/test_json_generate.rb b/tests/test_json_generate.rb
index 2110eba..a8741a5 100755
--- a/tests/test_json_generate.rb
+++ b/tests/test_json_generate.rb
@@ -43,6 +43,8 @@ EOT
def test_generate
json = generate(@hash)
assert_equal(JSON.parse(@json2), JSON.parse(json))
+ json = JSON[@hash]
+ assert_equal(JSON.parse(@json2), JSON.parse(json))
parsed_json = parse(json)
assert_equal(@hash, parsed_json)
json = generate({1=>2})