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

- (Packer) initialize(options = {}) - (Packer) initialize(io, options = {})

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:

  • - (Packer) initialize(options = {})

    Parameters:

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

    This packer writes serialzied 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

- (Object) buffer (readonly)

Internal buffer

Returns:

  • MessagePack::Buffer



72
73
74
# File 'doclib/msgpack/packer.rb', line 72

def buffer
  @buffer
end

Instance Method Details

- (Object) clear

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

Returns:

  • nil



126
127
# File 'doclib/msgpack/packer.rb', line 126

def clear
end

- (Boolean) empty?

Returns true if the internal buffer is empty. Same as buffer.empty?. This method is slightly faster than size.

Returns:

  • (Boolean)


143
144
# File 'doclib/msgpack/packer.rb', line 143

def empty?
end

- (Packer) flush

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

Returns:



118
119
# File 'doclib/msgpack/packer.rb', line 118

def flush
end

- (Object) register_type(type, klass) {|object| ... } - (Object) register_type(type, klass, method_name)

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:

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

    Parameters:

    • type (Fixnum)

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

    • klass (Class)

      Class to be serialized with speicifed type id

    Yield Parameters:

    • object (Object)

      object to be serialized

  • - (Object) register_type(type, klass, method_name)

    Parameters:

    • type (Fixnum)

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

    • klass (Class)

      Class to be serialized with speicifed 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

- (Object) registered_types

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

- (Integer) size

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

Returns:

  • (Integer)


134
135
# File 'doclib/msgpack/packer.rb', line 134

def size
end

- (Array) to_a

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



162
163
# File 'doclib/msgpack/packer.rb', line 162

def to_a
end

- (String) to_str Also known as: to_s

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

Returns:



151
152
# File 'doclib/msgpack/packer.rb', line 151

def to_str
end

- (Boolean) type_registered?(klass_or_type)

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

- (Packer) write(obj) 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

- (Packer) write_array_header(n)

Write a header of an array whose size is n. For example, write_array_header(1).write(true) is same as write([ true ]).

Returns:



100
101
# File 'doclib/msgpack/packer.rb', line 100

def write_array_header(n)
end

- (Packer) write_map_header(n)

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:



109
110
# File 'doclib/msgpack/packer.rb', line 109

def write_map_header(n)
end

- (Object) write_nil

Serializes a nil object. Same as write(nil).



91
92
# File 'doclib/msgpack/packer.rb', line 91

def write_nil
end

- (Integer) write_to(io)

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



173
174
# File 'doclib/msgpack/packer.rb', line 173

def write_to(io)
end