summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/json/common.rb1
-rw-r--r--lib/json/light_object.rb37
2 files changed, 38 insertions, 0 deletions
diff --git a/lib/json/common.rb b/lib/json/common.rb
index cf7a8b9..e8e76b6 100644
--- a/lib/json/common.rb
+++ b/lib/json/common.rb
@@ -1,4 +1,5 @@
require 'json/version'
+require 'json/light_object'
module JSON
class << self
diff --git a/lib/json/light_object.rb b/lib/json/light_object.rb
new file mode 100644
index 0000000..69dd5ab
--- /dev/null
+++ b/lib/json/light_object.rb
@@ -0,0 +1,37 @@
+require 'ostruct'
+
+module JSON
+ class LightObject < OpenStruct
+ class << self
+ alias [] new
+
+ def json_create(data)
+ data = data.dup
+ data.delete JSON.create_id
+ self[data]
+ end
+ end
+
+ def to_hash
+ __send__ :table
+ end
+
+ def |(other)
+ self.class[other.to_hash.merge(to_hash)]
+ end
+
+ def as_json(*)
+ to_hash | { JSON.create_id => self.class.name }
+ 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