Purpose
The core MessagePack API provides simple pack and unpack methods for serializing and deserializing Ruby objects to and from binary format.
References
-
API reference - Complete method documentation
-
Format specification - Binary format details
Concepts
Packing objects
Use Messagepack.pack to serialize Ruby objects to binary format:
Messagepack.pack({hello: "world"}) # => "\x81\xA5hello\xA5world"Where,
-
Messagepack.packaccepts any Ruby object as its argument -
The return value is a binary string containing the serialized data
-
Supported types include: nil, boolean, integer, float, string, array, hash, and any registered extension types
Unpacking data
Use Messagepack.unpack to deserialize binary data back to Ruby objects:
data = Messagepack.pack({hello: "world"})
Messagepack.unpack(data) # => {"hello"=>"world"}Where,
-
Messagepack.unpackaccepts a binary string or IO object -
The return value is the original Ruby object
-
Extra bytes after the deserialized object will raise a
Messagepack::MalformedFormatError
Supported types
Boolean
Messagepack.pack(true) # => "\xC3"
Messagepack.pack(false) # => "\xC2"
Messagepack.unpack("\xC3") # => true
Messagepack.unpack("\xC2") # => falseInteger
MessagePack supports integers from -2^63 to 2^64-1:
Messagepack.pack(0) # => "\x00"
Messagepack.pack(127) # => "\x7F"
Messagepack.pack(128) # => "\xCC\x80"
Messagepack.pack(-1) # => "\xFF"
Messagepack.unpack("\xCC\x80") # => 128Float
Messagepack.pack(3.14) # => "\xCB@\x09\x1E\xB8Q\xEB\x85\x1F"
Messagepack.unpack("\xCB@\x09\x1E\xB8Q\xEB\x85\x1F") # => 3.14String
Strings are encoded as UTF-8:
Messagepack.pack("hello") # => "\xA5hello"
Messagepack.unpack("\xA5hello") # => "hello"Examples
# Serialize a complex object
data = {
name: "Alice",
age: 30,
skills: ["Ruby", "Python"],
metadata: {
active: true,
score: 95.5
}
}
binary = Messagepack.pack(data)
# => "\x84\xA4name\xA5Alice\xA3age\x1E\xA6skills\
# \x92\xA4Ruby\xA6Python\xA8metadata\x82\xA6active\
# \xC3\xA5score\xCB@_\x00\x00"
# Deserialize back to a Ruby object
result = Messagepack.unpack(binary)
# => {"name"=>"Alice", "age"=>30, "skills"=>["Ruby", "Python"],
# "metadata"=>{"active"=>true, "score"=>95.5}}