API Documentation
Below is the documentation for all MsgPack API functions. For information on MsgPack, please see the package's README.
Basic Serialization/Deserialization
MsgPack.unpack — Function.unpack(bytes, T::Type = Any; strict::Tuple=())Return unpack(IOBuffer(bytes), T; strict=strict).
unpack(msgpack_byte_stream::IO, T::Type = Any; strict::Tuple=())Return the Julia value of type T deserialized from msgpack_byte_stream.
T is assumed to have valid msgpack_type and from_msgpack definitions.
If msgpack_type(T) === AnyType(), unpack will deserialize the next MessagePack object from msgpack_byte_stream into the default Julia representation corresponding to the object's MessagePack type. For details on default Julia representations, see AbstractMsgPackType.
The strict keyword argument is a Tuple of DataTypes where each element S must obey msgpack_type(S) === StructType(). unpack will assume that encountered MessagePack Maps to be deserialized to e.g. S will always contain fields that correspond strictly to the fields of S. In other words, the ith key in the Map must correspond to fieldname(S, i), and the ith value must correspond to getfield(::S, i).
MsgPack.pack — Function.pack(x)Serialize x to MessagePack format and return the resulting Vector{UInt8}.
This function uses msgpack_type and to_msgpack to determine the appropriate translation of the value into MessagePack format.
See also: unpack
Julia <–> MessagePack Conversion
MsgPack.msgpack_type — Function.msgpack_type(::Type{T}) where {T}Return an instance of the AbstractMsgPackType subtype corresponding to T's intended MessagePack representation. For example:
msgpack_type(::Type{UUID}) = StringType()If this method is overloaded such that msgpack_type(T) === M(), then to_msgpack(::M, ::T) and from_msgpack(::Type{T}, x) should also be overloaded to handle conversion of T instances to/from MsgPack-compatible types.
By default, this method returns AnyType(). While this fallback method need not be overloaded to support deserialization of T instances via unpack, msgpack_type(T) must be overloaded to return a non-AnyType AbstractMsgPackType instance in order to support serializing T instances via pack.
See also: from_msgpack, to_msgpack
MsgPack.to_msgpack — Function.to_msgpack(::M, value_to_serialize::T) where {M<:AbstractMsgPackType,T}Return an "M-compatible" representation of value_to_serialize (for compatibility definitions, see the docstrings for subtypes of AbstractMsgPackType).
By default, to_msgpack simply returns value_to_serialize directly.
The implementation of pack utilizes this function for every value encountered during serialization, calling it in a manner similar to the following psuedocode:
t = msgpack_type(T)
value_in_compatible_representation = to_msgpack(t, value_to_serialize::T)
_serialize_in_msgpack_format(t, value_in_compatible_representation)For example, if msgpack_type(UUID) was defined to return StringType(), an appropriate to_msgpack implementation might be:
to_msgpack(::StringType, uuid::UUID) = string(uuid)See also: from_msgpack, msgpack_type, AbstractMsgPackType
MsgPack.from_msgpack — Function.from_msgpack(::Type{T}, value_deserialized_by_msgpack) where {T}Return the value_deserialized_by_msgpack converted to type T. By default, this method simply calls convert(T, value_deserialized_by_msgpack).
The implementation of unpack calls this function on every deserialized value; in this case, T is generally derived from the type specified by the caller of unpack.
For example, if msgpack_type(UUID) was defined to return StringType(), an appropriate from_msgpack implementation might be:
from_msgpack(::Type{UUID}, uuid::AbstractString) = UUID(uuid)See also: to_msgpack, msgpack_type, AbstractMsgPackType
MsgPack.construct — Function.construct(T::Type, args...)Return an instance of T given args; defaults to T(args...).
This function is meant to be overloaded for types T where msgpack_type(T) === StructType().
This function is called by unpack when deserializing T objects. When called for this purpose, args are field values for the given T instance and are passed in the order specified by fieldname(T, i). If a given field of T wasn't found in the object's MessagePack Map representation, the corresponding value in args will be MsgPack.FieldNotFound().
Julia <–> MessagePack Interface Types
MsgPack.AbstractMsgPackType — Type.AbstractMsgPackTypeAn abstract type whose subtypes define a MessagePack <–> Julia type interface.
The subtypes of AbstractMsgPackType are:
MsgPack.IntegerType — Type.IntegerType <: AbstractMsgPackTypeA Julia type corresponding to the MessagePack Integer type.
If msgpack_type(T) is defined to return IntegerType(), then T must support:
to_msgpack(::IntegerType, ::T)::Sfrom_msgpack(::Type{T}, ::S)::T- standard numeric comparators (
>,<,==, etc.) against values of typeS
where S may be one of the following types:
UInt8UInt16UInt32UInt64Int8Int16Int32Int64
MsgPack.NilType — Type.NilType <: AbstractMsgPackTypeA Julia type corresponding to the MessagePack Nil type.
If msgpack_type(T) is defined to return NilType(), then T must support:
from_msgpack(::Type{T}, ::Nothing)::T
MsgPack.BooleanType — Type.BooleanType <: AbstractMsgPackTypeA Julia type corresponding to the MessagePack Boolean type.
If msgpack_type(T) is defined to return BooleanType(), then T must support:
to_msgpack(::BooleanType, ::T)::Boolfrom_msgpack(::Type{T}, ::Bool)::T
MsgPack.FloatType — Type.FloatType <: AbstractMsgPackTypeA Julia type corresponding to the MessagePack Float type.
If msgpack_type(T) is defined to return FloatType(), then T must support:
to_msgpack(::FloatType, ::T)::Sfrom_msgpack(::Type{T}, ::S)::T- standard numeric comparators (
>,<,==, etc.) against values of typeS
where S may be one of the following types:
Float32Float64
MsgPack.StringType — Type.StringType <: AbstractMsgPackTypeA Julia type corresponding to the MessagePack String type.
If msgpack_type(T) is defined to return StringType(), then T must support:
to_msgpack(::StringType, ::T)::Stringfrom_msgpack(::Type{T}, ::String)::T
MsgPack.BinaryType — Type.BinaryType <: AbstractMsgPackTypeA Julia type corresponding to the MessagePack Binary type.
If msgpack_type(T) is defined to return BinaryType(), then T must support:
to_msgpack(::BinaryType, ::T)::Vector{UInt8}from_msgpack(::Type{T}, ::Vector{UInt8})::T
MsgPack.ArrayType — Type.ArrayType <: AbstractMsgPackTypeA Julia type corresponding to the MessagePack Array type.
If msgpack_type(T) is defined to return ArrayType(), then T must support:
lengthiterateMsgPack._eltype(T)(falls back toeltype(T))
and/or must support:
to_msgpack(::ArrayType, ::T)::AbstractArrayfrom_msgpack(::Type{T}, ::Vector)::T
MsgPack.MapType — Type.MapType <: AbstractMsgPackTypeA Julia type corresponding to the MessagePack Map type.
If msgpack_type(T) is defined to return MapType(), then T must support:
lengthiterateMsgPack._keytype(T)(falls back tokeytype(T))MsgPack._valtype(T)(falls back tovaltype(T))
and/or must support:
to_msgpack(::ArrayType, ::T)::AbstractDictfrom_msgpack(::Type{T}, ::Dict)::T
MsgPack.ExtensionType — Type.ExtensionType <: AbstractMsgPackTypeA Julia type corresponding to the MessagePack Extension type.
If msgpack_type(T) is defined to return ExtensionType(), then T must support:
to_msgpack(::ExtensionType, ::T)::Extensionfrom_msgpack(::Type{T}, ::Extension)::T
See also: Extension
MsgPack.AnyType — Type.AnyType <: AbstractMsgPackTypeThe fallback return type of msgpack_type(::Type), indicating that the given Julia type does not have a known corresponding MessagePack type.
MsgPack.StructType — Type.StructType <: AbstractMsgPackTypeIf msgpack_type(T) is defined to return StructType, T will be (de)serialized as a MessagePack Map type, where each key-value pair corresponds to a field of T.
Tmust be an instantiable Julia type that supportsfieldcount,fieldtype,
fieldname, and getfield.
Tmust support a validconstructdefinition (supported by default
if T supports e.g. T((getfield(::T, i) for i in 1:fieldcount(T))...))
packwill always serializeT's fields in the order specified by
fieldname. In other words, it is generally guaranteed that bytes written by pack(x::T) can be unpacked via unpack(bytes, T; strict=(T,)).
View Types
MsgPack.ArrayView — Type.ArrayView{T} <: AbstractVector{T}A Julia struct that wraps a MessagePack byte buffer to provide an immutable view of the MessagePack Array stored within the wrapped byte buffer.
This type is intended to be utilized via unpack. For example, a call to arr = unpack(bytes, ArrayView{Dict{String,Int32}}) will generally return a value more quickly than arr = unpack(bytes, Vector{Dict{String,Int32}}); the latter will perform full deserialization immediately while the former will only scan over bytes to tag the positions of arr's elements, deferring the actual deserialization of these elements to the time of their access via arr[index].
Note that ArrayView does not implement any form of caching - repeat accesses of the same element will re-deserialize the element upon every access.
MsgPack.MapView — Type.MessagePack Extension Functionality
MsgPack.Extension — Type.struct Extension
type::Int8
data::Vector{UInt8}
endA wrapper for bytes formatted to the MessagePack Extension type specification.
See also: extserialize, extdeserialize
MsgPack.extserialize — Function.extserialize(type, x)Return Extension(type, data) where data is the result of calling Julia's built-in serialize function on x, facilitating direct serialization Julia values to MessagePack format:
julia> struct Point{T}
x::T
y::T
end
julia> val = [Point(rand(), rand()) for _ in 1:100];
julia> bytes = MsgPack.pack(MsgPack.extserialize(123, x));
julia> type, new_val = MsgPack.extdeserialize(MsgPack.unpack(bytes));
julia> type == 123
true
julia> new_val == val
trueNote that extserialize/extdeserialize are subject to the same caveat as Julia's built-in serialize/deserialize functions: "In general, this process will not work if the reading and writing are done by different versions of Julia, or an instance of Julia with a different system image.".
See also: Extension, extdeserialize
MsgPack.extdeserialize — Function.extdeserialize(x::Extension)Return (x.type, value) where value is the result of calling Julia's built-in deserialize function on x.data.
This function is meant to be used in conjuction with extserialize; see that function's docstring for more details.
See also: Extension, extserialize