Class: SDL4R::Serializer
- Inherits:
-
Object
- Object
- SDL4R::Serializer
- Defined in:
- lib/sdl4r/serializer.rb
Overview
Allows to serialize/deserialize between SDL and Ruby Objects.
Authors
Philippe Vosges
Instance Method Summary collapse
- #deserialize(tag, o = OpenStruct.new) ⇒ Object
- #serialize(o, tag = Tag.new(SDL4R::ROOT_TAG_NAME)) ⇒ Object
Instance Method Details
#deserialize(tag, o = OpenStruct.new) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/sdl4r/serializer.rb', line 57 def deserialize(tag, o = OpenStruct.new) is_open_struct_or_hash = o.is_a?(OpenStruct) or o.is_a?(Hash) # TODO Deserialization from anonymous child tags # Deserialization from values if tag.has_values? if tag.has_children? or tag.has_attributes? variable_value = tag.values if o.instance_variable_defined?("@value") or not o.instance_variable_defined?("@values") # value is preferred variable_name = "value" variable_value = variable_value[0] if variable_value.length == 1 else variable_name = "values" end if is_open_struct_or_hash or o.instance_variable_defined?(variable_name) set_object_variable(o, variable_name, variable_value) end else # the tag only has values return variable_value.length == 1 ? variable_value[0] : variable_value end end # Deserialization from attributes tag.attributes do |attribute_namespace, attribute_name, attribute_value| if is_open_struct_or_hash or o.instance_variable_defined?("@#{attribute_name}") set_object_variable(o, attribute_name, attribute_value) end end # Deserialization from (not anonymous) child tags tag.children do |child| # if child.namespace != '' or child.name != SDL4R::ANONYMOUS_TAG_NAME variable_name = "@#{child.name}" # Check wether this variable is assignable if is_open_struct_or_hash or o.instance_variable_defined?(variable_name) variable_value = nil if child.has_values? and not child.has_children? and not child.has_attributes? # If the object only has values (no children, no atttributes): # then the values are the variable value variable_value = child.values variable_value = variable_value[0] if variable_value.length == 1 else # Consider this tag as an object previous_value = o.instance_variable_get(variable_name) if previous_value.nil? variable_value = deserialize(child) elsif SDL4R::is_coercible?(previous_value) variable_value = deserialize(child) else variable_value = deserialize(child, previous_value) end end set_object_variable(o, child.name, variable_value) end # end end return o end |
#serialize(o, tag = Tag.new(SDL4R::ROOT_TAG_NAME)) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/sdl4r/serializer.rb', line 36 def serialize(o, tag = Tag.new(SDL4R::ROOT_TAG_NAME)) raise ArgumentError, '"tag" must be a Tag' unless tag.is_a?(Tag) o = o.marshal_dump if o.is_a? OpenStruct if o.is_a? Hash o.each_pair { |key, value| serialize_variable(key.to_s, value, tag) } else o.instance_variables.each { |name| value = o.instance_variable_get(name) name = name[1..name.size] if name =~ /^@/ serialize_variable(name, value, tag) } end tag end |