From 202ffe23354ae5b1b19c00b430e0ac323316e4b0 Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Wed, 31 Jan 2024 10:50:06 +0900 Subject: [PATCH] Make OpenStruct support as optional --- lib/json/add/ostruct.rb | 7 ++- lib/json/generic_object.rb | 7 ++- tests/json_addition_test.rb | 2 +- tests/json_generic_object_test.rb | 2 +- tests/json_parser_test.rb | 71 +++++++++++++++++-------------- 5 files changed, 50 insertions(+), 39 deletions(-) diff --git a/lib/json/add/ostruct.rb b/lib/json/add/ostruct.rb index f912e522..ed7df3bb 100644 --- a/lib/json/add/ostruct.rb +++ b/lib/json/add/ostruct.rb @@ -2,7 +2,10 @@ unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED require 'json' end -require 'ostruct' +begin + require 'ostruct' +rescue LoadError +end class OpenStruct @@ -48,4 +51,4 @@ def as_json(*) def to_json(*args) as_json.to_json(*args) end -end +end if defined?(::OpenStruct) diff --git a/lib/json/generic_object.rb b/lib/json/generic_object.rb index 108309db..b7b8b04e 100644 --- a/lib/json/generic_object.rb +++ b/lib/json/generic_object.rb @@ -1,5 +1,8 @@ #frozen_string_literal: false -require 'ostruct' +begin + require 'ostruct' +rescue LoadError +end module JSON class GenericObject < OpenStruct @@ -67,5 +70,5 @@ def as_json(*) def to_json(*a) as_json.to_json(*a) end - end + end if defined?(::OpenStruct) end diff --git a/tests/json_addition_test.rb b/tests/json_addition_test.rb index 2877bef7..3a7a5817 100644 --- a/tests/json_addition_test.rb +++ b/tests/json_addition_test.rb @@ -190,7 +190,7 @@ def test_ostruct # XXX this won't work; o.foo = { :bar => true } o.foo = { 'bar' => true } assert_equal o, parse(JSON(o), :create_additions => true) - end + end if defined?(::OpenStruct) def test_set s = Set.new([:a, :b, :c, :a]) diff --git a/tests/json_generic_object_test.rb b/tests/json_generic_object_test.rb index c4d39120..d6d7e308 100644 --- a/tests/json_generic_object_test.rb +++ b/tests/json_generic_object_test.rb @@ -79,4 +79,4 @@ def switch_json_creatable ensure JSON::GenericObject.json_creatable = false end -end +end if defined?(JSON::GenericObject) diff --git a/tests/json_parser_test.rb b/tests/json_parser_test.rb index cc9a74a9..49c7c856 100644 --- a/tests/json_parser_test.rb +++ b/tests/json_parser_test.rb @@ -3,7 +3,10 @@ require_relative 'test_helper' require 'stringio' require 'tempfile' -require 'ostruct' +begin + require 'ostruct' +rescue LoadError +end begin require 'bigdecimal' rescue LoadError @@ -412,21 +415,6 @@ def self.json_create(o) end end - class SubOpenStruct < OpenStruct - def [](k) - __send__(k) - end - - def []=(k, v) - @item_set = true - __send__("#{k}=", v) - end - - def item_set? - @item_set - end - end - def test_parse_object_custom_hash_derived_class res = parse('{"foo":"bar"}', :object_class => SubHash) assert_equal({"foo" => "bar"}, res) @@ -434,24 +422,41 @@ def test_parse_object_custom_hash_derived_class assert res.item_set? end - def test_parse_object_custom_non_hash_derived_class - res = parse('{"foo":"bar"}', :object_class => SubOpenStruct) - assert_equal "bar", res.foo - assert_equal(SubOpenStruct, res.class) - assert res.item_set? - end + if defined?(::OpenStruct) + class SubOpenStruct < OpenStruct + def [](k) + __send__(k) + end - def test_parse_generic_object - res = parse( - '{"foo":"bar", "baz":{}}', - :object_class => JSON::GenericObject - ) - assert_equal(JSON::GenericObject, res.class) - assert_equal "bar", res.foo - assert_equal "bar", res["foo"] - assert_equal "bar", res[:foo] - assert_equal "bar", res.to_hash[:foo] - assert_equal(JSON::GenericObject, res.baz.class) + def []=(k, v) + @item_set = true + __send__("#{k}=", v) + end + + def item_set? + @item_set + end + end + + def test_parse_object_custom_non_hash_derived_class + res = parse('{"foo":"bar"}', :object_class => SubOpenStruct) + assert_equal "bar", res.foo + assert_equal(SubOpenStruct, res.class) + assert res.item_set? + end + + def test_parse_generic_object + res = parse( + '{"foo":"bar", "baz":{}}', + :object_class => JSON::GenericObject + ) + assert_equal(JSON::GenericObject, res.class) + assert_equal "bar", res.foo + assert_equal "bar", res["foo"] + assert_equal "bar", res[:foo] + assert_equal "bar", res.to_hash[:foo] + assert_equal(JSON::GenericObject, res.baz.class) + end end def test_generate_core_subclasses_with_new_to_json