Class: MessagePack::Packer
- Inherits:
-
Object
- Object
- MessagePack::Packer
- Defined in:
- doclib/msgpack/packer.rb
Overview
MessagePack::Packer is a class to serialize objects.
Instance Attribute Summary collapse
-
#buffer ⇒ Object
readonly
Internal buffer.
Instance Method Summary collapse
-
#empty? ⇒ Boolean
Returns true if the internal buffer is empty.
-
#flush ⇒ Packer
Flushes data in the internal buffer to the internal IO.
-
#initialize(*args) ⇒ Packer
constructor
Creates a MessagePack::Packer instance.
-
#register_type(type, klass, method_name, &block) ⇒ Object
Register a new ext type to serialize it.
-
#registered_types ⇒ Object
Returns a list of registered types, ordered by type id.
-
#reset ⇒ Object
(also: #clear)
Makes the internal buffer empty.
-
#size ⇒ Integer
Returns size of the internal buffer.
-
#to_a ⇒ Array
Returns content of the internal buffer as an array of strings.
-
#to_str ⇒ String
(also: #to_s)
Returns all data in the buffer as a string.
-
#type_registered?(klass_or_type) ⇒ Boolean
Returns true/false which indicate specified class or type id is registered or not.
-
#write(obj) ⇒ Packer
(also: #pack)
Serializes an object into internal buffer, and flushes to io if necessary.
-
#write_array_header(n) ⇒ Packer
Write a header of an array whose size is n.
-
#write_bin(obj) ⇒ Object
Serializes a string object as binary data.
-
#write_bin_header(n) ⇒ Packer
Write a header of a binary string whose size is n.
-
#write_float32(value) ⇒ Packer
Serializes value as 32-bit single precision float into internal buffer.
-
#write_map_header(n) ⇒ Packer
Write a header of an map whose size is n.
-
#write_nil ⇒ Object
Serializes a nil object.
-
#write_to(io) ⇒ Integer
Writes all of data in the internal buffer into the given IO.
Constructor Details
#initialize(options = {}) ⇒ Packer #initialize(io, options = {}) ⇒ Packer
Creates a MessagePack::Packer instance. See Buffer#initialize for supported options.
Supported options:
-
:compatibility_mode serialize in older versions way, without str8 and bin types
See also Buffer#initialize for other options.
26 27 |
# File 'doclib/msgpack/packer.rb', line 26 def initialize(*args) end |
Instance Attribute Details
#buffer ⇒ Object (readonly)
Internal buffer
72 73 74 |
# File 'doclib/msgpack/packer.rb', line 72 def buffer @buffer end |
Instance Method Details
#empty? ⇒ Boolean
Returns true if the internal buffer is empty. Same as buffer.empty?. This method is slightly faster than size.
176 177 |
# File 'doclib/msgpack/packer.rb', line 176 def empty? end |
#flush ⇒ Packer
Flushes data in the internal buffer to the internal IO. Same as _buffer.flush. If internal IO is not set, it does nothing.
150 151 |
# File 'doclib/msgpack/packer.rb', line 150 def flush end |
#register_type(type, klass) {|object| ... } ⇒ Object #register_type(type, klass, method_name) ⇒ Object
Register a new ext type to serialize it. This method should be called with one of method name or block, which returns bytes(ASCII-8BIT String) representation of object to be serialized.
46 47 |
# File 'doclib/msgpack/packer.rb', line 46 def register_type(type, klass, method_name, &block) end |
#registered_types ⇒ Object
Returns a list of registered types, ordered by type id. Each element is a Hash object includes keys :type, :class and :packer.
55 56 |
# File 'doclib/msgpack/packer.rb', line 55 def registered_types end |
#reset ⇒ Object Also known as: clear
Makes the internal buffer empty. Same as buffer.clear.
158 159 |
# File 'doclib/msgpack/packer.rb', line 158 def reset end |
#size ⇒ Integer
Returns size of the internal buffer. Same as buffer.size.
167 168 |
# File 'doclib/msgpack/packer.rb', line 167 def size end |
#to_a ⇒ Array
Returns content of the internal buffer as an array of strings. Same as buffer.to_a. This method is faster than to_str.
195 196 |
# File 'doclib/msgpack/packer.rb', line 195 def to_a end |
#to_str ⇒ String Also known as: to_s
Returns all data in the buffer as a string. Same as buffer.to_str.
184 185 |
# File 'doclib/msgpack/packer.rb', line 184 def to_str end |
#type_registered?(klass_or_type) ⇒ Boolean
Returns true/false which indicate specified class or type id is registered or not.
64 65 |
# File 'doclib/msgpack/packer.rb', line 64 def type_registered?(klass_or_type) end |
#write(obj) ⇒ Packer Also known as: pack
Serializes an object into internal buffer, and flushes to io if necessary.
If it could not serialize the object, it raises NoMethodError: undefined method `to_msgpack' for #<the_object>.
83 84 |
# File 'doclib/msgpack/packer.rb', line 83 def write(obj) end |
#write_array_header(n) ⇒ Packer
Write a header of an array whose size is n. For example, write_array_header(1).write(true) is same as write([ true ]).
106 107 |
# File 'doclib/msgpack/packer.rb', line 106 def write_array_header(n) end |
#write_bin(obj) ⇒ Object
Serializes a string object as binary data. Same as write(“string”.encode(Encoding::BINARY)).
97 98 |
# File 'doclib/msgpack/packer.rb', line 97 def write_bin(obj) end |
#write_bin_header(n) ⇒ Packer
Write a header of a binary string whose size is n. Useful if you want to append large binary data without loading it into memory at once. For example, MessagePack::Packer.new(io).write_bin_header(12).flush io.write('chunk1') io.write('chunk2') is the same as write('chunk1chunk2'.encode(Encoding::BINARY)).
129 130 |
# File 'doclib/msgpack/packer.rb', line 129 def write_bin_header(n) end |
#write_float32(value) ⇒ Packer
Serializes value as 32-bit single precision float into internal buffer. value will be approximated with the nearest possible single precision float, thus being potentially lossy. However, the serialized string will only take up 5 bytes instead of 9 bytes compared to directly serializing a 64-bit double precision Ruby Float.
141 142 |
# File 'doclib/msgpack/packer.rb', line 141 def write_float32(value) end |
#write_map_header(n) ⇒ Packer
Write a header of an map whose size is n. For example, write_map_header(1).write('key').write(true) is same as write('key'=>true).
115 116 |
# File 'doclib/msgpack/packer.rb', line 115 def write_map_header(n) end |
#write_nil ⇒ Object
Serializes a nil object. Same as write(nil).
91 92 |
# File 'doclib/msgpack/packer.rb', line 91 def write_nil end |
#write_to(io) ⇒ Integer
Writes all of data in the internal buffer into the given IO. Same as buffer.write_to(io). This method consumes and removes data from the internal buffer. io must respond to write(data) method.
206 207 |
# File 'doclib/msgpack/packer.rb', line 206 def write_to(io) end |