Class: MessagePack::Unpacker
- Inherits:
-
Object
- Object
- MessagePack::Unpacker
- Defined in:
- doclib/msgpack/unpacker.rb
Overview
MessagePack::Unpacker is a class to deserialize objects.
Instance Attribute Summary collapse
-
#buffer ⇒ MessagePack::Buffer
readonly
Internal buffer.
Instance Method Summary collapse
-
#each {|object| ... } ⇒ Object
Repeats to deserialize objects.
-
#feed(data) ⇒ Unpacker
Appends data into the internal buffer.
-
#feed_each(data) {|object| ... } ⇒ Object
Appends data into the internal buffer and repeats to deserialize objects.
-
#initialize(*args) ⇒ Unpacker
constructor
Creates a MessagePack::Unpacker instance.
-
#read ⇒ Object
(also: #unpack)
Deserializes an object from the io or internal buffer and returns it.
-
#read_array_header ⇒ Integer
Read a header of an array and returns its size.
-
#read_map_header ⇒ Integer
Reads a header of an map and returns its size.
-
#register_type(type, klass, method_name, &block) ⇒ Object
Register a new ext type to deserialize it.
-
#registered_types ⇒ Object
Returns a list of registered types, ordered by type id.
-
#reset ⇒ Object
Clears the internal buffer and resets deserialization state of the unpacker.
-
#skip ⇒ Object
Deserializes an object and ignores it.
-
#skip_nil ⇒ Boolean
Deserializes a nil value if it exists and returns true.
-
#type_registered?(klass_or_type) ⇒ Boolean
Returns true/false which indicate specified class or type id is registered or not.
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.
28 29 |
# File 'doclib/msgpack/unpacker.rb', line 28 def initialize(*args) end |
Instance Attribute Details
#buffer ⇒ MessagePack::Buffer (readonly)
Internal buffer
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.
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).
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 { … }.
173 174 |
# File 'doclib/msgpack/unpacker.rb', line 173 def feed_each(data, &block) end |
#read ⇒ Object 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.
88 89 |
# File 'doclib/msgpack/unpacker.rb', line 88 def read end |
#read_array_header ⇒ Integer
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.
124 125 |
# File 'doclib/msgpack/unpacker.rb', line 124 def read_array_header end |
#read_map_header ⇒ Integer
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.
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.
46 47 |
# File 'doclib/msgpack/unpacker.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 :unpacker.
55 56 |
# File 'doclib/msgpack/unpacker.rb', line 55 def registered_types end |
#reset ⇒ Object
Clears the internal buffer and resets deserialization state of the unpacker.
181 182 |
# File 'doclib/msgpack/unpacker.rb', line 181 def reset end |
#skip ⇒ Object
Deserializes an object and ignores it. This method is faster than read.
This method could raise the same errors with read.
100 101 |
# File 'doclib/msgpack/unpacker.rb', line 100 def skip end |
#skip_nil ⇒ Boolean
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.
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.
64 65 |
# File 'doclib/msgpack/unpacker.rb', line 64 def type_registered?(klass_or_type) end |