Skip to content

Commit a6c01e1

Browse files
committed
Remove JSON::GenericObject
1 parent 25a196d commit a6c01e1

5 files changed

Lines changed: 20 additions & 158 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- Removed `JSON.restore`.
1717
- Removed `JSON::PRETTY_STATE_PROTOTYPE`.
1818
- Removed the insecure `create_additions` option.
19+
- Removed `JSON::GenericObject`.
1920

2021
### 2026-07-31 (2.21.2)
2122

Gemfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ gemspec
55
group :development do
66
gem "ruby_memcheck" if RUBY_PLATFORM =~ /linux/i
77
gem "bigdecimal"
8-
gem "ostruct"
98
gem "rake"
109
gem "rake-compiler"
1110
gem "test-unit"

lib/json/generic_object.rb

Lines changed: 0 additions & 60 deletions
This file was deleted.

test/json/json_generic_object_test.rb

Lines changed: 0 additions & 63 deletions
This file was deleted.

test/json/json_parser_test.rb

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
require_relative 'test_helper'
33
require 'stringio'
44
require 'tempfile'
5-
begin
6-
require 'ostruct'
7-
rescue LoadError
8-
end
95
begin
106
require 'bigdecimal'
117
rescue LoadError
@@ -772,44 +768,33 @@ def test_parse_object_custom_hash_derived_class
772768
assert res.item_set?
773769
end
774770

775-
if defined?(::OpenStruct)
776-
class SubOpenStruct < OpenStruct
777-
def [](k)
778-
__send__(k)
779-
end
780-
781-
def []=(k, v)
782-
@item_set = true
783-
__send__("#{k}=", v)
784-
end
771+
class OpenStructLike
772+
def initialize
773+
@attrs = {}
774+
end
785775

786-
def item_set?
787-
@item_set
788-
end
776+
def [](k)
777+
@attrs[k.to_sym]
789778
end
790779

791-
def test_parse_object_custom_non_hash_derived_class
792-
res = parse('{"foo":"bar"}', :object_class => SubOpenStruct)
793-
assert_equal "bar", res.foo
794-
assert_equal "bar", res[:foo]
795-
assert_equal(SubOpenStruct, res.class)
796-
assert res.item_set?
780+
def []=(k, v)
781+
@attrs[k.to_sym] = v
797782
end
798783

799-
def test_parse_generic_object
800-
res = parse(
801-
'{"foo":"bar", "baz":{}}',
802-
:object_class => JSON::GenericObject
803-
)
804-
assert_equal(JSON::GenericObject, res.class)
805-
assert_equal "bar", res.foo
806-
assert_equal "bar", res["foo"]
807-
assert_equal "bar", res[:foo]
808-
assert_equal "bar", res.to_hash[:foo]
809-
assert_equal(JSON::GenericObject, res.baz.class)
784+
def method_missing(name, ...)
785+
@attrs.fetch(name) do
786+
super
787+
end
810788
end
811789
end
812790

791+
def test_parse_object_custom_non_hash_derived_class
792+
res = parse('{"foo":"bar"}', :object_class => OpenStructLike)
793+
assert_equal "bar", res.foo
794+
assert_equal "bar", res[:foo]
795+
assert_equal(OpenStructLike, res.class)
796+
end
797+
813798
def test_generate_core_subclasses_with_default_to_json
814799
assert_equal '{"foo":"bar"}', JSON(SubHash["foo" => "bar"])
815800
assert_equal '["foo"]', JSON(SubArray["foo"])

0 commit comments

Comments
 (0)