Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Unreleased

* Deprecate `JSON::State#[]` and `JSON::State#[]=`. Consider using `JSON::Coder` instead.
* `JSON::Coder` now also yields to the block when encountering strings with invalid encoding.
* Fix GeneratorError messages to be UTF-8 encoded.
* Fix memory leak when `Exception` is raised, or `throw` is used during JSON generation.
Expand Down
4 changes: 4 additions & 0 deletions lib/json/ext/generator/state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ def to_h
#
# Returns the value returned by method +name+.
def [](name)
::JSON.deprecation_warning("JSON::State#[] is deprecated and will be removed in json 3.0.0")

if respond_to?(name)
__send__(name)
else
Expand All @@ -87,6 +89,8 @@ def [](name)
#
# Sets the attribute name to value.
def []=(name, value)
::JSON.deprecation_warning("JSON::State#[]= is deprecated and will be removed in json 3.0.0")

if respond_to?(name_writer = "#{name}=")
__send__ name_writer, value
else
Expand Down
4 changes: 4 additions & 0 deletions lib/json/truffle_ruby/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,8 @@ def generate_new(obj, anIO = nil) # :nodoc:

# Return the value returned by method +name+.
def [](name)
::JSON.deprecation_warning("JSON::State#[] is deprecated and will be removed in json 3.0.0")

if respond_to?(name)
__send__(name)
else
Expand All @@ -448,6 +450,8 @@ def [](name)
end

def []=(name, value)
::JSON.deprecation_warning("JSON::State#[]= is deprecated and will be removed in json 3.0.0")

if respond_to?(name_writer = "#{name}=")
__send__ name_writer, value
else
Expand Down
50 changes: 29 additions & 21 deletions test/json/json_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ def test_states
assert_equal('{"1":2}', json)
s = JSON.state.new
assert s.check_circular?
assert s[:check_circular?]
assert_deprecated_warning(/JSON::State/) do
assert s[:check_circular?]
end
h = { 1=>2 }
h[3] = h
assert_raise(JSON::NestingError) { generate(h) }
Expand All @@ -193,7 +195,9 @@ def test_states
a << a
assert_raise(JSON::NestingError) { generate(a, s) }
assert s.check_circular?
assert s[:check_circular?]
assert_deprecated_warning(/JSON::State/) do
assert s[:check_circular?]
end
end

def test_falsy_state
Expand Down Expand Up @@ -375,28 +379,32 @@ def to_s
end

def test_hash_likeness_set_symbol
state = JSON.state.new
assert_equal nil, state[:foo]
assert_equal nil.class, state[:foo].class
assert_equal nil, state['foo']
state[:foo] = :bar
assert_equal :bar, state[:foo]
assert_equal :bar, state['foo']
state_hash = state.to_hash
assert_kind_of Hash, state_hash
assert_equal :bar, state_hash[:foo]
assert_deprecated_warning(/JSON::State/) do
state = JSON.state.new
assert_equal nil, state[:foo]
assert_equal nil.class, state[:foo].class
assert_equal nil, state['foo']
state[:foo] = :bar
assert_equal :bar, state[:foo]
assert_equal :bar, state['foo']
state_hash = state.to_hash
assert_kind_of Hash, state_hash
assert_equal :bar, state_hash[:foo]
end
end

def test_hash_likeness_set_string
state = JSON.state.new
assert_equal nil, state[:foo]
assert_equal nil, state['foo']
state['foo'] = :bar
assert_equal :bar, state[:foo]
assert_equal :bar, state['foo']
state_hash = state.to_hash
assert_kind_of Hash, state_hash
assert_equal :bar, state_hash[:foo]
assert_deprecated_warning(/JSON::State/) do
state = JSON.state.new
assert_equal nil, state[:foo]
assert_equal nil, state['foo']
state['foo'] = :bar
assert_equal :bar, state[:foo]
assert_equal :bar, state['foo']
state_hash = state.to_hash
assert_kind_of Hash, state_hash
assert_equal :bar, state_hash[:foo]
end
end

def test_json_state_to_h_roundtrip
Expand Down
Loading