Module: MessagePack
- Defined in:
- lib/right_agent/serialize/message_pack.rb
Overview
Extend MessagePack to conform to load/dump interface of other serializers like JSON and to create internal class objects when they are encountered when unserializing
Class Method Summary collapse
-
.create(object) ⇒ Object
Create any msgpack_class objects nested within the unserialized data by calling their associated msgpack_create method.
-
.dump(object) ⇒ Object
Serialize object Any non-standard objects must have an associated to_msgpack instance method.
-
.load(data) ⇒ Object
Unserialize data and generate any msgpack_class objects within.
Class Method Details
.create(object) ⇒ Object
Create any msgpack_class objects nested within the unserialized data by calling their associated msgpack_create method
Parameters
- object(Object)
-
Unserialized object that may contain other objects that need to be created
Return
- object(Object)
-
Fully unserialized object
Raises
- ArgumentError
-
If object to be created does not have a msgpack_create method
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/right_agent/serialize/message_pack.rb', line 57 def self.create(object) if object.is_a?(Hash) object.each { |k, v| object[k] = create(v) if v.is_a?(Hash) || v.is_a?(Array) } if klass_name = object['msgpack_class'] klass = deep_const_get(klass_name) raise ArgumentError, "#{klass_name} missing msgpack_create method" unless klass.respond_to?(:msgpack_create) object = klass.msgpack_create(object) end elsif object.is_a?(Array) object = object.map { |v| v.is_a?(Hash) || v.is_a?(Array) ? create(v) : v } end object end |
.dump(object) ⇒ Object
Serialize object Any non-standard objects must have an associated to_msgpack instance method
Parameters
- object(Object)
-
Object to be serialized
Return
- (String)
-
Serialized object
79 80 81 |
# File 'lib/right_agent/serialize/message_pack.rb', line 79 def self.dump(object) pack(object) end |
.load(data) ⇒ Object
Unserialize data and generate any msgpack_class objects within
Parameters
- data(String)
-
MessagePack string to unserialize
Return
- obj(Object)
-
Unserialized object
37 38 39 40 41 42 43 44 |
# File 'lib/right_agent/serialize/message_pack.rb', line 37 def self.load(data) if data.respond_to?(:force_encoding) # For Ruby 1.9 need to ensure that MessagePack receives ASCII-8BIT data create(unpack(data.force_encoding("ASCII-8BIT"))) else create(unpack(data)) end end |