summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGES5
-rw-r--r--lib/json/common.rb6
-rw-r--r--lib/json/pure/generator.rb7
-rwxr-xr-xtests/test_json_generate.rb45
4 files changed, 59 insertions, 4 deletions
diff --git a/CHANGES b/CHANGES
index 41944e6..137572b 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,3 +1,8 @@
+2010-08-07 (1.4.5)
+ * Manage data structure nesting depth in state object during generation. This
+ should reduce problems with to_json method definŃ–tions that only have one
+ argument.
+ * Some fixes in the state objects and additional tests.
2010-08-06 (1.4.4)
* Fixes build problem for rubinius under OS X, http://github.com/flori/json/issues/closed#issue/25
* Fixes crashes described in http://github.com/flori/json/issues/closed#issue/21 and
diff --git a/lib/json/common.rb b/lib/json/common.rb
index c6a31c1..b530670 100644
--- a/lib/json/common.rb
+++ b/lib/json/common.rb
@@ -63,20 +63,20 @@ 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
+ )
end
# Returns the JSON generator modul, that is used by JSON. This might be
diff --git a/lib/json/pure/generator.rb b/lib/json/pure/generator.rb
index 0584108..12959bf 100644
--- a/lib/json/pure/generator.rb
+++ b/lib/json/pure/generator.rb
@@ -163,6 +163,10 @@ module JSON
# the generated JSON, max_nesting = 0 if no maximum is checked.
attr_accessor :max_nesting
+ # This integer returns the current depth data structure nesting in the
+ # generated JSON.
+ attr_accessor :depth
+
def check_max_nesting(depth) # :nodoc:
return if @max_nesting.zero?
current_nesting = depth + 1
@@ -196,6 +200,7 @@ module JSON
@array_nl = opts[:array_nl] if opts.key?(:array_nl)
@allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan)
@ascii_only = opts[:ascii_only] if opts.key?(:ascii_only)
+ @depth = opts[:depth] || 0
if !opts.key?(:max_nesting) # defaults to 19
@max_nesting = 19
elsif opts[:max_nesting]
@@ -210,7 +215,7 @@ module JSON
# passed to the configure method.
def to_h
result = {}
- for iv in %w[indent space space_before object_nl array_nl allow_nan max_nesting]
+ for iv in %w[indent space space_before object_nl array_nl allow_nan max_nesting ascii_only depth]
result[iv.intern] = instance_variable_get("@#{iv}")
end
result
diff --git a/tests/test_json_generate.rb b/tests/test_json_generate.rb
index 26ca1ad..8dc804b 100755
--- a/tests/test_json_generate.rb
+++ b/tests/test_json_generate.rb
@@ -102,6 +102,51 @@ EOT
assert s[:check_circular?]
end
+ def test_pretty_state
+ state = PRETTY_STATE_PROTOTYPE.dup
+ assert_equal({
+ :allow_nan => false,
+ :array_nl => "\n",
+ :ascii_only => false,
+ :depth => 0,
+ :indent => " ",
+ :max_nesting => 19,
+ :object_nl => "\n",
+ :space => " ",
+ :space_before => "",
+ }.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s })
+ end
+
+ def test_safe_state
+ state = SAFE_STATE_PROTOTYPE.dup
+ assert_equal({
+ :allow_nan => false,
+ :array_nl => "",
+ :ascii_only => false,
+ :depth => 0,
+ :indent => "",
+ :max_nesting => 19,
+ :object_nl => "",
+ :space => "",
+ :space_before => "",
+ }.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s })
+ end
+
+ def test_fast_state
+ state = FAST_STATE_PROTOTYPE.dup
+ assert_equal({
+ :allow_nan => false,
+ :array_nl => "",
+ :ascii_only => false,
+ :depth => 0,
+ :indent => "",
+ :max_nesting => 0,
+ :object_nl => "",
+ :space => "",
+ :space_before => "",
+ }.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s })
+ end
+
def test_allow_nan
assert_raises(GeneratorError) { generate([JSON::NaN]) }
assert_equal '[NaN]', generate([JSON::NaN], :allow_nan => true)