aboutsummaryrefslogtreecommitdiffstats
path: root/ext/json/lib/json/generic_object.rb
diff options
context:
space:
mode:
authorhsbt <hsbt@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-04-12 08:36:37 +0000
committerhsbt <hsbt@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-04-12 08:36:37 +0000
commit2e4f0af00f85ca228bcf5fa919882359411c652a (patch)
tree6317ef3d0c352a8d7496139f34277e224fe3d4ac /ext/json/lib/json/generic_object.rb
parent7b14512bee222bdb6392e865b00ef4b4c42b9364 (diff)
downloadruby-2e4f0af00f85ca228bcf5fa919882359411c652a.tar.gz
* ext/json/*, test/json/*: Reverted r50231. Because it's not works with
cross-compile environment. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50267 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/json/lib/json/generic_object.rb')
-rw-r--r--ext/json/lib/json/generic_object.rb62
1 files changed, 62 insertions, 0 deletions
diff --git a/ext/json/lib/json/generic_object.rb b/ext/json/lib/json/generic_object.rb
new file mode 100644
index 0000000000..3a14f0ea7d
--- /dev/null
+++ b/ext/json/lib/json/generic_object.rb
@@ -0,0 +1,62 @@
+require 'ostruct'
+
+module JSON
+ class GenericObject < OpenStruct
+ class << self
+ alias [] new
+
+ def json_creatable?
+ @json_creatable
+ end
+
+ attr_writer :json_creatable
+
+ def json_create(data)
+ data = data.dup
+ data.delete JSON.create_id
+ self[data]
+ end
+
+ def from_hash(object)
+ case
+ when object.respond_to?(:to_hash)
+ result = new
+ object.to_hash.each do |key, value|
+ result[key] = from_hash(value)
+ end
+ result
+ when object.respond_to?(:to_ary)
+ object.to_ary.map { |a| from_hash(a) }
+ else
+ object
+ end
+ end
+
+ def load(source, proc = nil, opts = {})
+ result = ::JSON.load(source, proc, opts.merge(:object_class => self))
+ result.nil? ? new : result
+ end
+
+ def dump(obj, *args)
+ ::JSON.dump(obj, *args)
+ end
+ end
+ self.json_creatable = false
+
+ def to_hash
+ table
+ end
+
+ def |(other)
+ self.class[other.to_hash.merge(to_hash)]
+ end
+
+ def as_json(*)
+ { JSON.create_id => self.class.name }.merge to_hash
+ end
+
+ def to_json(*a)
+ as_json.to_json(*a)
+ end
+ end
+end