Skip to content

Commit e9fbc89

Browse files
committed
Deprecate JSON::State#[] and JSON::State#[]=
This prevent from freezing and sharing state instances. If you needs some sort of arguments or extra state to the generator methods, consider using `JSON::Coder` instead.
1 parent 826cb2a commit e9fbc89

File tree

4 files changed

+38
-21
lines changed

4 files changed

+38
-21
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### Unreleased
44

5+
* Deprecate `JSON::State#[]` and `JSON::State#[]=`. Consider using `JSON::Coder` instead.
56
* `JSON::Coder` now also yields to the block when encountering strings with invalid encoding.
67
* Fix GeneratorError messages to be UTF-8 encoded.
78
* Fix memory leak when `Exception` is raised, or `throw` is used during JSON generation.

lib/json/ext/generator/state.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ def to_h
7575
#
7676
# Returns the value returned by method +name+.
7777
def [](name)
78+
::JSON.deprecation_warning("JSON::State#[] is deprecated and will be removed in json 3.0.0")
79+
7880
if respond_to?(name)
7981
__send__(name)
8082
else
@@ -87,6 +89,8 @@ def [](name)
8789
#
8890
# Sets the attribute name to value.
8991
def []=(name, value)
92+
::JSON.deprecation_warning("JSON::State#[]= is deprecated and will be removed in json 3.0.0")
93+
9094
if respond_to?(name_writer = "#{name}=")
9195
__send__ name_writer, value
9296
else

lib/json/truffle_ruby/generator.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,8 @@ def generate_new(obj, anIO = nil) # :nodoc:
439439

440440
# Return the value returned by method +name+.
441441
def [](name)
442+
::JSON.deprecation_warning("JSON::State#[] is deprecated and will be removed in json 3.0.0")
443+
442444
if respond_to?(name)
443445
__send__(name)
444446
else
@@ -448,6 +450,8 @@ def [](name)
448450
end
449451

450452
def []=(name, value)
453+
::JSON.deprecation_warning("JSON::State#[]= is deprecated and will be removed in json 3.0.0")
454+
451455
if respond_to?(name_writer = "#{name}=")
452456
__send__ name_writer, value
453457
else

test/json/json_generator_test.rb

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,9 @@ def test_states
183183
assert_equal('{"1":2}', json)
184184
s = JSON.state.new
185185
assert s.check_circular?
186-
assert s[:check_circular?]
186+
assert_deprecated_warning(/JSON::State/) do
187+
assert s[:check_circular?]
188+
end
187189
h = { 1=>2 }
188190
h[3] = h
189191
assert_raise(JSON::NestingError) { generate(h) }
@@ -193,7 +195,9 @@ def test_states
193195
a << a
194196
assert_raise(JSON::NestingError) { generate(a, s) }
195197
assert s.check_circular?
196-
assert s[:check_circular?]
198+
assert_deprecated_warning(/JSON::State/) do
199+
assert s[:check_circular?]
200+
end
197201
end
198202

199203
def test_falsy_state
@@ -375,28 +379,32 @@ def to_s
375379
end
376380

377381
def test_hash_likeness_set_symbol
378-
state = JSON.state.new
379-
assert_equal nil, state[:foo]
380-
assert_equal nil.class, state[:foo].class
381-
assert_equal nil, state['foo']
382-
state[:foo] = :bar
383-
assert_equal :bar, state[:foo]
384-
assert_equal :bar, state['foo']
385-
state_hash = state.to_hash
386-
assert_kind_of Hash, state_hash
387-
assert_equal :bar, state_hash[:foo]
382+
assert_deprecated_warning(/JSON::State/) do
383+
state = JSON.state.new
384+
assert_equal nil, state[:foo]
385+
assert_equal nil.class, state[:foo].class
386+
assert_equal nil, state['foo']
387+
state[:foo] = :bar
388+
assert_equal :bar, state[:foo]
389+
assert_equal :bar, state['foo']
390+
state_hash = state.to_hash
391+
assert_kind_of Hash, state_hash
392+
assert_equal :bar, state_hash[:foo]
393+
end
388394
end
389395

390396
def test_hash_likeness_set_string
391-
state = JSON.state.new
392-
assert_equal nil, state[:foo]
393-
assert_equal nil, state['foo']
394-
state['foo'] = :bar
395-
assert_equal :bar, state[:foo]
396-
assert_equal :bar, state['foo']
397-
state_hash = state.to_hash
398-
assert_kind_of Hash, state_hash
399-
assert_equal :bar, state_hash[:foo]
397+
assert_deprecated_warning(/JSON::State/) do
398+
state = JSON.state.new
399+
assert_equal nil, state[:foo]
400+
assert_equal nil, state['foo']
401+
state['foo'] = :bar
402+
assert_equal :bar, state[:foo]
403+
assert_equal :bar, state['foo']
404+
state_hash = state.to_hash
405+
assert_kind_of Hash, state_hash
406+
assert_equal :bar, state_hash[:foo]
407+
end
400408
end
401409

402410
def test_json_state_to_h_roundtrip

0 commit comments

Comments
 (0)