0

I am trying to deserialize a MsgPack packet that I have in a byte array like this:

47 52 209 0 144 209 0 144 72 86 54 195 209 66 73

And I want to get it into an object of this type:

public class Info
{
    public byte wX;
    public byte wY;
    public Int16 wMXCount;
    public Int16 wMYCount;
    public byte wMZCount;
    public byte wMRegionZ;
    public byte wZ;

    public bool m;
    public Int16 tkn;

}

I am using MsgPack.CLI to try to deserialize it as seen here.

static public T Deserialize<T>(byte[] bytes)
{
    var serializer = MessagePackSerializer.Get<T>();
    using (var byteStream = new MemoryStream(bytes))
    {
        return serializer.Unpack(byteStream);
    }
}

and then run it with

Info mi2;
mi2 = Deserialize<Info>(bytes); //bytes is the byte array that contains the MsgPack

Unfortunately mi2 is just like a newly created Info and does not contain any of the data from the MsgPacket. It would be really great if someone has an idea of how to get this working.

Thanks a lot!

Update

I made a dummy Info and tried to emulate something similar to the data above and serializing it myself. What I get is first of all a 153 in front (id for fixed array with 9 elements) which makes sense, I had forgotten that, but also the elements are in a different order.

It appears they are in alphabetical order! Is there a way to map/deserialize them like they are ordered in the class or do I have to change the names of my fields to awX, bwY, cwMXCount... Is there a flag somewhere to deserialize them in a specific way?

Community
  • 1
  • 1
Lucahk
  • 21
  • 4
  • Well-written first question. But rather than editing it to include the answer therein, it's better to [answer your own question](http://stackoverflow.com/help/self-answer). That way other readers can tell quickly that the issue is resolved. – dbc Oct 03 '16 at 21:20
  • @dbc Thank you. Didn't know answering your own queston was possible and was looking for a "solved" button. I have submitted an answer now and will mark it as solution as soon as I can – Lucahk Oct 04 '16 at 13:44

1 Answers1

2

Fixed it! For future reference, this is how: Apparently there is no standard for ordering so I had to set the orders of my fields manually. This solved all my problems:

public class Info
{
    [MessagePackMember(0)]
    public byte wX;
    [MessagePackMember(1)]
    public byte wY;
    [MessagePackMember(2)]
    public Int16 wMXCount;
...
Lucahk
  • 21
  • 4