Class: Sawyer::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/sawyer/serializer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format, dump_method_name = nil, load_method_name = nil) ⇒ Serializer

Public: Wraps a serialization format for Sawyer. Nested objects are prepared for serialization (such as changing Times to ISO 8601 Strings). Any serialization format that responds to #dump and #load will work.



37
38
39
40
41
# File 'lib/sawyer/serializer.rb', line 37

def initialize(format, dump_method_name = nil, load_method_name = nil)
  @format = format
  @dump = @format.method(dump_method_name || :dump)
  @load = @format.method(load_method_name || :load)
end

Class Method Details

.any_jsonObject



6
7
8
# File 'lib/sawyer/serializer.rb', line 6

def self.any_json
  yajl || multi_json || json
end

.jsonObject



16
17
18
19
20
# File 'lib/sawyer/serializer.rb', line 16

def self.json
  require 'json'
  new(JSON)
rescue LoadError
end

.message_packObject



28
29
30
31
32
# File 'lib/sawyer/serializer.rb', line 28

def self.message_pack
  require 'msgpack'
  new(MessagePack, :pack, :unpack)
rescue LoadError
end

.multi_jsonObject



22
23
24
25
26
# File 'lib/sawyer/serializer.rb', line 22

def self.multi_json
  require 'multi_json'
  new(MultiJson)
rescue LoadError
end

.yajlObject



10
11
12
13
14
# File 'lib/sawyer/serializer.rb', line 10

def self.yajl
  require 'yajl'
  new(Yajl)
rescue LoadError
end

Instance Method Details

#decode(data) ⇒ Object Also known as: load

Public: Decodes a String into an Object (usually a Hash or Array of Hashes).

data - An encoded String.

Returns a decoded Object.



60
61
62
63
# File 'lib/sawyer/serializer.rb', line 60

def decode(data)
  return nil if data.nil? || data.empty?
  decode_object(@load.call(data))
end

#decode_hash(hash) ⇒ Object



94
95
96
97
98
99
# File 'lib/sawyer/serializer.rb', line 94

def decode_hash(hash)
  hash.keys.each do |key|
    hash[key.to_sym] = decode_hash_value(key, hash.delete(key))
  end
  hash
end

#decode_hash_value(key, value) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/sawyer/serializer.rb', line 101

def decode_hash_value(key, value)
  if time_field?(key, value)
    Time.parse(value)
  elsif value.is_a?(Hash)
    decode_hash(value)
  elsif value.is_a?(Array)
    value.map { |o| decode_hash_value(key, o) }
  else
    value
  end
end

#decode_object(data) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/sawyer/serializer.rb', line 86

def decode_object(data)
  case data
  when Hash then decode_hash(data)
  when Array then data.map { |o| decode_object(o) }
  else data
  end
end

#encode(data) ⇒ Object Also known as: dump

Public: Encodes an Object (usually a Hash or Array of Hashes).

data - Object to be encoded.

Returns an encoded String.



48
49
50
# File 'lib/sawyer/serializer.rb', line 48

def encode(data)
  @dump.call(encode_object(data))
end

#encode_hash(hash) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/sawyer/serializer.rb', line 75

def encode_hash(hash)
  hash.keys.each do |key|
    case value = hash[key]
    when Date then hash[key] = value.to_time.utc.xmlschema
    when Time then hash[key] = value.utc.xmlschema
    when Hash then hash[key] = encode_hash(value)
    end
  end
  hash
end

#encode_object(data) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/sawyer/serializer.rb', line 67

def encode_object(data)
  case data
  when Hash then encode_hash(data)
  when Array then data.map { |o| encode_object(o) }
  else data
  end
end

#time_field?(key, value) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/sawyer/serializer.rb', line 113

def time_field?(key, value)
  value && key =~ /_(at|on)$/
end