Module: Serialize

Defined in:
lib/simple_mapper/support/bliss_serializer.rb

Constant Summary collapse

XML_OPTIONS =
{
  :include_key => false, # Can be false, :element, or :attribute
  :report_nil => true, # Sets an attribute nil="true" on elements that are nil, so that the reader doesn't read as an empty string
  :key_name => :id, # Default key name
  :instance_columns => :visible_properties,
  :class_columns => :visible_properties,
  :default_root => 'xml',
}
JSON_OPTIONS =
{
  :include_key => false, # Can be true or false
  :key_name => :id, # Default key name
  :instance_columns => :visible_properties,
  :class_columns => :visible_properties,
}

Class Method Summary collapse

Class Method Details

.hash_from_json(json, options = {}) ⇒ Object



78
79
80
# File 'lib/simple_mapper/support/bliss_serializer.rb', line 78

def self.hash_from_json(json,options={})
  json.to_s.formatted(:json).to_hash
end

.hash_from_xml(xml, options = {}) ⇒ Object



56
57
58
# File 'lib/simple_mapper/support/bliss_serializer.rb', line 56

def self.hash_from_xml(xml,options={})
  xml.to_s.formatted(:xml).to_hash
end

.object_to_json(obj, options = {}) ⇒ Object



66
67
68
69
70
# File 'lib/simple_mapper/support/bliss_serializer.rb', line 66

def self.object_to_json(obj, options={})
  options = options.reverse_merge!(obj.class.json_options) if obj.class.respond_to?(:json_options)
  options = options.reverse_merge!(obj.json_options) if obj.respond_to?(:json_options)
  to_json(obj.to_hash(options), options)
end

.object_to_xml(obj, options = {}) ⇒ Object



17
18
19
20
21
# File 'lib/simple_mapper/support/bliss_serializer.rb', line 17

def self.object_to_xml(obj, options={})
  options = options.reverse_merge!(obj.class.xml_options) if obj.class.respond_to?(:xml_options)
  options = options.reverse_merge!(obj.xml_options) if obj.respond_to?(:xml_options)
  to_xml(obj.to_hash(options), {:root => Inflector.underscore(obj.class.name)}.merge(options))
end

.to_json(attributes = {}, options = {}) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/simple_mapper/support/bliss_serializer.rb', line 71

def self.to_json(attributes={}, options={})
  attributes = attributes.to_hash if attributes.respond_to?(:to_hash)
  options = JSON_OPTIONS.merge(options)
  options[:exclude] = [options[:exclude]].flatten.compact.collect {|e| e.to_s}
  attributes.reject! {|k,v| options[:exclude].include?(k)}
  options[:root] ? JSON.unparse({options[:root] => attributes}) : JSON.unparse(attributes)
end

.to_xml(attributes = {}, options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/simple_mapper/support/bliss_serializer.rb', line 22

def self.to_xml(attributes={}, options={})
  options = XML_OPTIONS.merge(options)
  root = options[:root]
  attributes.reject! {|k,v| options[:exclude].include?(k)}

  doc = REXML::Document.new
  root_element = doc.add_element(root || 'xml')

  case options[:include_key]
  when :attribute
    root_element.add_attribute(options[:key_name].to_s, attributes.delete(options[:key_name].to_s).to_s).extended do
      def self.to_string; %Q[#@expanded_name="#{to_s().gsub(/"/, '"')}"] end
    end
  when :element
    root_element.add_element(options[:key_name].to_s) << REXML::Text.new(attributes.delete(options[:key_name].to_s).to_s)
  end

  attributes.each do |key,value|
    if value.nil?
      node = root_element.add_element(key.to_s)
      node.add_attribute('nil', 'true') if options[:report_nil]
    else
      if value.respond_to?(:to_xml)
        assoc_options = {}
        assoc_options = {:exclude => value.association.foreign_key_column.name} if value.respond_to?(:association)
        root_element.add_element(REXML::Document.new(value.to_xml(assoc_options.merge(:root => key.to_s)).to_s))
      else
        root_element.add_element(key.to_s) << REXML::Text.new(value.to_s.dup)
      end
    end
  end

  root ? doc.to_s : doc.children[0].children.to_s
end