summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGES2
-rw-r--r--lib/json/common.rb2
-rw-r--r--lib/json/generic_object.rb (renamed from lib/json/light_object.rb)14
-rwxr-xr-xtests/test_json.rb8
-rw-r--r--tests/test_json_generic_object.rb35
5 files changed, 46 insertions, 15 deletions
diff --git a/CHANGES b/CHANGES
index b347a7c..37c3c80 100644
--- a/CHANGES
+++ b/CHANGES
@@ -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)
diff --git a/lib/json/common.rb b/lib/json/common.rb
index e8e76b6..a30e4ce 100644
--- a/lib/json/common.rb
+++ b/lib/json/common.rb
@@ -1,5 +1,5 @@
require 'json/version'
-require 'json/light_object'
+require 'json/generic_object'
module JSON
class << self
diff --git a/lib/json/light_object.rb b/lib/json/generic_object.rb
index 07eeecf..7f3dbbd 100644
--- a/lib/json/light_object.rb
+++ b/lib/json/generic_object.rb
@@ -1,7 +1,7 @@
require 'ostruct'
module JSON
- class LightObject < OpenStruct
+ class GenericObject < OpenStruct
class << self
alias [] new
@@ -17,11 +17,11 @@ module JSON
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)
@@ -29,17 +29,11 @@ module JSON
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
diff --git a/tests/test_json.rb b/tests/test_json.rb
index 4b4bc55..22cd5ee 100755
--- a/tests/test_json.rb
+++ b/tests/test_json.rb
@@ -316,14 +316,14 @@ class TestJSON < Test::Unit::TestCase
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
diff --git a/tests/test_json_generic_object.rb b/tests/test_json_generic_object.rb
new file mode 100644
index 0000000..e13a492
--- /dev/null
+++ b/tests/test_json_generic_object.rb
@@ -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