Class: MessagePack::Packer

Inherits:
Object
  • Object
show all
Defined in:
doclib/msgpack/packer.rb

Overview

MessagePack::Packer is a class to serialize objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Overloads:

  • #initialize(options = {}) ⇒ Packer

    Parameters:

    • options (Hash) (defaults to: {})
  • #initialize(io, options = {}) ⇒ Packer

    This packer writes serialized objects into the IO when the internal buffer is filled. io must respond to write(string) or append(string) method.

    Parameters:

    • io (IO)
    • options (Hash) (defaults to: {})


26
27
# File 'doclib/msgpack/packer.rb', line 26

def initialize(*args)
end

Instance Attribute Details

#bufferObject (readonly)

Internal buffer

Returns:

  • MessagePack::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.

Returns:

  • (Boolean)


176
177
# File 'doclib/msgpack/packer.rb', line 176

def empty?
end

#flushPacker

Flushes data in the internal buffer to the internal IO. Same as _buffer.flush. If internal IO is not set, it does nothing.

Returns:



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.

Overloads:

  • #register_type(type, klass) {|object| ... } ⇒ Object

    Parameters:

    • type (Fixnum)

      type id (0-127) user defined type id for specified Class

    • klass (Class)

      Class to be serialized with specified type id

    Yield Parameters:

    • object (Object)

      object to be serialized

  • #register_type(type, klass, method_name) ⇒ Object

    Parameters:

    • type (Fixnum)

      type id (0-127) user defined type id for specified Class

    • klass (Class)

      Class to be serialized with specified type id

    • method_name (Symbol)

      method which returns bytes of serialized representation

Returns:

  • nil



46
47
# File 'doclib/msgpack/packer.rb', line 46

def register_type(type, klass, method_name, &block)
end

#registered_typesObject

Returns a list of registered types, ordered by type id. Each element is a Hash object includes keys :type, :class and :packer.

Returns:

  • Array



55
56
# File 'doclib/msgpack/packer.rb', line 55

def registered_types
end

#resetObject Also known as: clear

Makes the internal buffer empty. Same as buffer.clear.

Returns:

  • nil



158
159
# File 'doclib/msgpack/packer.rb', line 158

def reset
end

#sizeInteger

Returns size of the internal buffer. Same as buffer.size.

Returns:

  • (Integer)


167
168
# File 'doclib/msgpack/packer.rb', line 167

def size
end

#to_aArray

Returns content of the internal buffer as an array of strings. Same as buffer.to_a. This method is faster than to_str.

Returns:

  • (Array)

    array of strings



195
196
# File 'doclib/msgpack/packer.rb', line 195

def to_a
end

#to_strString Also known as: to_s

Returns all data in the buffer as a string. Same as buffer.to_str.

Returns:



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.

Parameters:

  • klass_or_type (Class or Fixnum)

    Class or type id (0-127) to be checked

Returns:

  • (Boolean)

    true or false



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>.

Parameters:

  • obj (Object)

    object to serialize

Returns:



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 ]).

Returns:



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)).

Returns:



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.

Parameters:

  • value (Numeric)

Returns:



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).

Returns:



115
116
# File 'doclib/msgpack/packer.rb', line 115

def write_map_header(n)
end

#write_nilObject

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.

Parameters:

  • io (IO)

Returns:

  • (Integer)

    byte size of written data



206
207
# File 'doclib/msgpack/packer.rb', line 206

def write_to(io)
end