Skip to content

Commit

Permalink
Add JSON::GenericObject
Browse files Browse the repository at this point in the history
  • Loading branch information
flori committed Apr 28, 2012
1 parent 93b31b8 commit 2e402dc
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
2012-04-28 (1.7.0)
* Add JSON::GenericObject for method access to objects transmitted via JSON.
2012-04-27 (1.6.7)
* Fix possible crash when trying to parse nil value.
2012-02-11 (1.6.6)
Expand Down
2 changes: 1 addition & 1 deletion lib/json/common.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'json/version'
require 'json/light_object'
require 'json/generic_object'

module JSON
class << self
Expand Down
14 changes: 4 additions & 10 deletions lib/json/light_object.rb → lib/json/generic_object.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'ostruct'

module JSON
class LightObject < OpenStruct
class GenericObject < OpenStruct
class << self
alias [] new

Expand All @@ -17,29 +17,23 @@ def to_hash
end

def [](name)
to_hash[name.to_sym]
table[name.to_sym]
end

def []=(name, value)
modifiable[name.to_sym] = value
__send__ "#{name}=", value
end

def |(other)
self.class[other.to_hash.merge(to_hash)]
end

def as_json(*)
to_hash | { JSON.create_id => self.class.name }
{ JSON.create_id => self.class.name }.merge to_hash
end

def to_json(*a)
as_json.to_json(*a)
end

def method_missing(*a, &b)
to_hash.__send__(*a, &b)
rescue NoMethodError
super
end
end
end
8 changes: 4 additions & 4 deletions tests/test_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -316,14 +316,14 @@ def test_parse_object_custom_non_hash_derived_class
assert res.item_set?
end

def test_parse_light_object
res = parse('{"foo":"bar", "baz":{}}', :object_class => JSON::LightObject)
assert_equal(JSON::LightObject, res.class)
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::LightObject, res.baz.class)
assert_equal(JSON::GenericObject, res.baz.class)
end

def test_generate_core_subclasses_with_new_to_json
Expand Down
35 changes: 35 additions & 0 deletions tests/test_json_generic_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

require 'test/unit'
require File.join(File.dirname(__FILE__), 'setup_variant')
class TestJSONGenericObject < Test::Unit::TestCase
include JSON

def setup
@go = GenericObject[ :a => 1, :b => 2 ]
end

def test_attributes
assert_equal 1, @go.a
assert_equal 1, @go[:a]
assert_equal 2, @go.b
assert_equal 2, @go[:b]
assert_nil @go.c
assert_nil @go[:c]
end

def test_generate_json
assert_equal @go, JSON(JSON(@go))
end

def test_parse_json
assert_equal @go, l = JSON('{ "json_class": "JSON::GenericObject", "a": 1, "b": 2 }')
assert_equal 1, l.a
assert_equal @go, l = JSON('{ "a": 1, "b": 2 }', :object_class => GenericObject)
assert_equal 1, l.a
assert_equal GenericObject[:a => GenericObject[:b => 2]],
l = JSON('{ "a": { "b": 2 } }', :object_class => GenericObject)
assert_equal 2, l.a.b
end
end

0 comments on commit 2e402dc

Please sign in to comment.