summaryrefslogtreecommitdiff
path: root/ext/json/lib/json/generic_object.rb
diff options
context:
space:
mode:
Diffstat (limited to 'ext/json/lib/json/generic_object.rb')
-rw-r--r--ext/json/lib/json/generic_object.rb22
1 files changed, 22 insertions, 0 deletions
diff --git a/ext/json/lib/json/generic_object.rb b/ext/json/lib/json/generic_object.rb
index 7f3dbbd..8b1074c 100644
--- a/ext/json/lib/json/generic_object.rb
+++ b/ext/json/lib/json/generic_object.rb
@@ -5,12 +5,34 @@ module JSON
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
end
+ self.json_creatable = false
def to_hash
table