Module: Bridge::Serializer
- Defined in:
- lib/serializer.rb
Overview
:nodoc: all
Defined Under Namespace
Classes: Callback
Class Method Summary collapse
- .serialize(bridge, obj) ⇒ Object
- .unserialize(bridge, obj) ⇒ Object
- .unserialize_helper(bridge, obj, k, v) ⇒ Object
Class Method Details
.serialize(bridge, obj) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/serializer.rb', line 7 def self.serialize bridge, obj # Serialize immediately if obj responds to to_dict if obj.respond_to? :to_dict result = obj.to_dict # Enumerate hash and serialize each member elsif obj.is_a? Hash result = {} obj.each do |k, v| result[k] = serialize bridge, v end # Enumerate array and serialize each member elsif obj.is_a? Array result = obj.map do |v| serialize bridge, v end # Store as callback if callable elsif obj.respond_to?(:call) result = bridge.store_object(Callback.new(obj), ['callback']).to_dict # Return obj itself is JSON serializable elsif JSON::Ext::Generator::GeneratorMethods.constants.include? obj.class.name.to_sym result = obj # Otherwise store as service. Obj is a class instance or module else result = bridge.store_object(obj, Util.find_ops(obj)).to_dict end return result end |
.unserialize(bridge, obj) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/serializer.rb', line 35 def self.unserialize bridge, obj if obj.is_a? Hash obj.each do |k, v| unserialize_helper bridge, obj, k, v end elsif obj.is_a? Array obj.each_with_index do |v, k| unserialize_helper bridge, obj, k, v end end end |
.unserialize_helper(bridge, obj, k, v) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/serializer.rb', line 47 def self.unserialize_helper bridge, obj, k, v if v.is_a? Hash # If object has ref key, convert to reference if v.has_key? 'ref' # Create reference ref = Reference.new(bridge, v['ref'], v['operations']) if v.has_key? 'operations' and v['operations'].length == 1 and v['operations'][0] == 'callback' # Callback wrapper obj[k] = Util.ref_callback ref else obj[k] = ref end return end end unserialize bridge, v end |