References
-
Extension types reference - Symbol extension documentation
Concepts
Registering symbol type
factory.register_type(0, Symbol)Where,
-
0is the type ID for symbols -
The extension uses
to_symandto_sfor packing/unpacking
Symbol serialization
factory.register_type(0, Symbol)
binary = factory.pack(:hello_symbol)
result = factory.unpack(binary) # => :hello_symbolWhere,
-
Symbols are serialized as their string representation
-
Deserialization converts the string back to a symbol
-
This is more efficient than serializing as strings
Examples
Example 1. Symbol serialization in data structures
factory = Messagepack::Factory.new
factory.register_type(0, Symbol)
data = {
status: :active,
priority: :high,
tags: [:important, :urgent]
}
binary = factory.pack(data)
result = factory.unpack(binary)
# => {:status=>:active, :priority=>:high, :tags=>[:important, :urgent]}| The symbol extension is not registered by default. You must register it explicitly if you want to preserve symbols when serializing. |