MsgPack fails to unpack a basic structure

While trying out some MsgPack.jl functionnalities, I tested to pack/unpack a simple structure possessing two fields : one integer and one matrix. Following the documentation for this package, I obtained this code

using MsgPack

struct TestStruct
    x::Int64
    arr::Matrix{Float64}
end
MsgPack.msgpack_type(::Type{TestStruct}) = MsgPack.StructType()

test = TestStruct(3,rand(Float64,3,4))


test_packed = pack(test)
test2 = unpack(test_packed,strict=(TestStruct,))

Which returns

julia> test2 = unpack(test_packed,strict=(TestStruct,))
Dict{Any,Any} with 2 entries:
  "arr" => Any[0.198004, 0.870654, 0.0119973, 0.979071, 0.670376, 0.977469, 0.373035, 0.635284, 0.457175, 0.461499, 0.0531387, 0.307494]
  "x"   => 0x03

So basically my array field is returned as a vector and my integer is not correctly decoded. Not to mention that my structure is returned as a Dict.

What am I missing here ?

I guess there is two different problems here:

First, you need to provide the outermost type, the strict param is only for performance. e.g:

test2 = unpack(test_packed, TestStruct)

works when I use Vector instead of Matrix, but for your original example it throws

MethodError: no method matching Matrix{Float64}(::Vector{Float64})
...

Suggesting that Matrix is not supported (just a guess, I am not an expert).

Note: I wrote TypeParsers.jl to securely parse types from strings. I needed it to deserialize the outermost type (“TestStruct”, sent as a string prefix to the MsgPack data).

Thanks, that is the behavior I observed too, I am quite surprised that such transfer protocols do not handle matrix object, but I guess that is because I am too much used to scientific programming and not general programming…

Actually, I tested this communication process with MessagePack-CSharp where using the Typeless version of the serialization commands, the type is also passed direclty inside the message. So no need to send another message before that in this case.
After that, I just compare the type of the message with the list of types exported by my package.
If I switch to another solution, I will check TypeParsers more thoroughly.

As for Matrices, there is no support for complex numbers at the moment, which is a bit of a bummer for me but maybe somone will have an idea to support this via extension types, who knows…

1 Like