pythonc#serializationmsgpack

Deserialize MessagePack data in Python generated in .NET with struct


I have a program which serialises a large amount of data (450 data points) in .NET using MessagePack with a Struct. I can deserialise the data in Python but I can not obtain the key structure in Python.

.NET

byte[] statusBytes = Decompress(compressedData);

StatusT statusObject = new StatusT();

using (var ms = new MemoryStream(statusBytes))
{
    MessagePackSerializer<StatusT> serializer = MessagePackSerializer.Get<StatusT>();
    statusObject = serializer.Unpack(ms);
}

Python

bStripped = compressedData[4:]

statusBytes= gzip.decompress(bStripped)

return msgpack.unpackb(statusBytes)

This gives me a list which has ultimately ~450 elements (once flattened) and I can not obtain the key structure from this. Eg:

Python output

>> [[0, 0, 0], [0, 'BubbleDisable', ['connected', 0], 0, 7, 0, 59, 11, [0], 0, 0, 937], ... [224,['connected', 0], 6, 224]]

I can not figure out how to obtain the headers(/keys) for the deserialised data in Python, using the struct StatusT definition given in C#.


Solution

  • Solution

    Use CLI pythonnet library:

    from pythonnet import load
    load('netfx')
    import clr
    clr.AddReference(<path_to_dll>) # for both MsgPack and <my_dotNET_library>
    
    from MsgPack.Serialization import MessagePackSerializer
    from System.IO import MemoryStream
    from <my_dotNET_library> import StatusT
    
    serializer = MessagePackSerializer.Get[StatusT]()
    stream = MemoryStream(bData) # bData is <bytes object>
    statusObject = self.serializer.Unpack(stream)