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

  • :freeze freeze the deserialized objects. Can allow string deduplication and some allocation elision.

  • :key_cache Enable caching of map keys, this can improve performance significantly if the same map keys are frequently encountered, but also degrade performance if that’s not the case.

  • :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: {})


28
29
# File 'doclib/msgpack/unpacker.rb', line 28

def initialize(*args)
end

Instance Attribute Details

#bufferMessagePack::Buffer (readonly)

Internal buffer

Returns:



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

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



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

def each(&block)
end

#feed(data) ⇒ Unpacker

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

Parameters:

Returns:



146
147
# File 'doclib/msgpack/unpacker.rb', line 146

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



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

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



88
89
# File 'doclib/msgpack/unpacker.rb', line 88

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



124
125
# File 'doclib/msgpack/unpacker.rb', line 124

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



136
137
# File 'doclib/msgpack/unpacker.rb', line 136

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



46
47
# File 'doclib/msgpack/unpacker.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 :unpacker.

Returns:

  • Array



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

def registered_types
end

#resetObject

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

Returns:

  • nil



181
182
# File 'doclib/msgpack/unpacker.rb', line 181

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



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

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)


112
113
# File 'doclib/msgpack/unpacker.rb', line 112

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



64
65
# File 'doclib/msgpack/unpacker.rb', line 64

def type_registered?(klass_or_type)
end