Class: MessagePack::Unpacker

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

Overview

MessagePack::Unpacker is a class to deserialize objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Unpacker #initialize(io, options = {}) ⇒ Unpacker

Creates a MessagePack::Unpacker instance.

Supported options:

  • :symbolize_keys deserialize keys of Hash objects as Symbol instead of String

  • :allow_unknown_ext allow to deserialize ext type object with unknown type id as ExtensionValue instance. Otherwise (by default), unpacker throws UnknownExtTypeError.

See also Buffer#initialize for other options.

Overloads:

  • #initialize(options = {}) ⇒ Unpacker

    Parameters:

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

    This unpacker reads data from the io to fill the internal buffer. io must respond to readpartial(length [,string]) or read(length [,string]) method.

    Parameters:

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


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

def initialize(*args)
end

Instance Attribute Details

#bufferMessagePack::Buffer (readonly)

Internal buffer

Returns:



70
71
72
# File 'doclib/msgpack/unpacker.rb', line 70

def buffer
  @buffer
end

Instance Method Details

#each {|object| ... } ⇒ Object

Repeats to deserialize objects.

It repeats until the io or internal buffer does not include any complete objects.

If an IO is set, it repeats to read data from the IO when the buffer becomes empty until the IO raises EOFError.

This method could raise same errors with read excepting EOFError.

Yield Parameters:

  • object (Object)

    deserialized object

Returns:

  • nil



160
161
# File 'doclib/msgpack/unpacker.rb', line 160

def each(&block)
end

#feed(data) ⇒ Unpacker

Appends data into the internal buffer. This method is equivalent to unpacker.buffer.append(data).

Parameters:

Returns:



144
145
# File 'doclib/msgpack/unpacker.rb', line 144

def feed(data)
end

#feed_each(data) {|object| ... } ⇒ Object

Appends data into the internal buffer and repeats to deserialize objects. This method is equivalent to unpacker.feed(data) && unpacker.each { … }.

Parameters:

Yield Parameters:

  • object (Object)

    deserialized object

Returns:

  • nil



171
172
# File 'doclib/msgpack/unpacker.rb', line 171

def feed_each(data, &block)
end

#readObject Also known as: unpack

Deserializes an object from the io or internal buffer and returns it.

This method reads data from io into the internal buffer and deserializes an object from the buffer. It repeats reading data from the io until enough data is available to deserialize at least one object. After deserializing one object, unused data is left in the internal buffer.

If there're not enough data to deserialize one object, this method raises EOFError. If data format is invalid, this method raises MessagePack::MalformedFormatError. If the object nests too deeply, this method raises MessagePack::StackError.

Returns:

  • (Object)

    deserialized object



86
87
# File 'doclib/msgpack/unpacker.rb', line 86

def read
end

#read_array_headerInteger

Read a header of an array and returns its size. It converts a serialized array into a stream of elements.

If the serialized object is not an array, it raises MessagePack::UnexpectedTypeError. If there're not enough data, this method raises EOFError.

Returns:

  • (Integer)

    size of the array



122
123
# File 'doclib/msgpack/unpacker.rb', line 122

def read_array_header
end

#read_map_headerInteger

Reads a header of an map and returns its size. It converts a serialized map into a stream of key-value pairs.

If the serialized object is not a map, it raises MessagePack::UnexpectedTypeError. If there're not enough data, this method raises EOFError.

Returns:

  • (Integer)

    size of the map



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

def read_map_header
end

#register_type(type) {|data| ... } ⇒ Object #register_type(type, klass, class_method_name) ⇒ Object

Register a new ext type to deserialize it. This method should be called with Class and its class method name, or block, which returns a instance object.

Overloads:

  • #register_type(type) {|data| ... } ⇒ Object

    Parameters:

    • type (Fixnum)

      type id (0-127) user defined type id for specified deserializer block

    Yield Parameters:

    • data (String)

      bytes(ASCII-8BIT String) represents serialized object, to be deserialized

  • #register_type(type, klass, class_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

    • class_method_name (Symbol)

      class method which returns an instance object

Returns:

  • nil



44
45
# File 'doclib/msgpack/unpacker.rb', line 44

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

Returns:

  • Array



53
54
# File 'doclib/msgpack/unpacker.rb', line 53

def registered_types
end

#resetObject

Clears the internal buffer and resets deserialization state of the unpacker.

Returns:

  • nil



179
180
# File 'doclib/msgpack/unpacker.rb', line 179

def reset
end

#skipObject

Deserializes an object and ignores it. This method is faster than read.

This method could raise the same errors with read.

Returns:

  • nil



98
99
# File 'doclib/msgpack/unpacker.rb', line 98

def skip
end

#skip_nilBoolean

Deserializes a nil value if it exists and returns true. Otherwise, if a byte exists but the byte doesn't represent nil value, returns false.

If there're not enough data, this method raises EOFError.

Returns:

  • (Boolean)


110
111
# File 'doclib/msgpack/unpacker.rb', line 110

def skip_nil
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



62
63
# File 'doclib/msgpack/unpacker.rb', line 62

def type_registered?(klass_or_type)
end