Skip to content

Commit

Permalink
Merge pull request #565 from flori/optional-ostruct
Browse files Browse the repository at this point in the history
Make OpenStruct support as optional
  • Loading branch information
hsbt authored Jan 31, 2024
2 parents 7864324 + 202ffe2 commit 92c91e2
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 39 deletions.
7 changes: 5 additions & 2 deletions lib/json/add/ostruct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -48,4 +51,4 @@ def as_json(*)
def to_json(*args)
as_json.to_json(*args)
end
end
end if defined?(::OpenStruct)
7 changes: 5 additions & 2 deletions lib/json/generic_object.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#frozen_string_literal: false
require 'ostruct'
begin
require 'ostruct'
rescue LoadError
end

module JSON
class GenericObject < OpenStruct
Expand Down Expand Up @@ -67,5 +70,5 @@ def as_json(*)
def to_json(*a)
as_json.to_json(*a)
end
end
end if defined?(::OpenStruct)
end
2 changes: 1 addition & 1 deletion tests/json_addition_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
2 changes: 1 addition & 1 deletion tests/json_generic_object_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ def switch_json_creatable
ensure
JSON::GenericObject.json_creatable = false
end
end
end if defined?(JSON::GenericObject)
71 changes: 38 additions & 33 deletions tests/json_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -412,46 +415,48 @@ 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)
assert_equal(SubHash, res.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
Expand Down

0 comments on commit 92c91e2

Please sign in to comment.