msgpack / msgpack-cli

MessagePack implementation for Common Language Infrastructure / msgpack.org[C#]
http://msgpack.org
Apache License 2.0
834 stars 174 forks source link

Need Deserialization Example #313

Open benlemond opened 5 years ago

benlemond commented 5 years ago

I am having a problem understanding the syntax of this project. I am trying to deserialize a complex object and things keep getting out of order. I have the code deserializing to the high level "MsgPackMsg" object appropriately. However, the content of "PlcData" is not deserializing appropriately. I am not sure the best practice way of doing this on a nested complex object so an example would be great!!

 [MessagePackEnum]
    public enum MsgPackMsgType
    {
        [MessagePackEnumMember]
        PlcData,
        [MessagePackEnumMember]
        LogData,
    };
    [MessagePackEnum]
    public enum PlcDataMsgType
    {
        [MessagePackEnumMember]
        PlcInput,
        [MessagePackEnumMember]
        PlcOutput
    };
    [MessagePackEnum]
    public enum ZoneStatus
    {
        [MessagePackEnumMember]
        UNKNOWN,
        [MessagePackEnumMember]
        WARNING,
        [MessagePackEnumMember]
        SHUTDOWN,
        [MessagePackEnumMember]
        SAFE
    }    
    public class MsgPackMsg
    {
        [MessagePackMember(0)]
        public UInt64 timestamp;
        [MessagePackMember(1)]
        public string version;
        [MessagePackMember(2)]
        public MsgPackMsgType msg_type;
        [MessagePackMember(3)]
        public PlcData data;
    }   
    public class PlcData
    {
        [MessagePackMember(0)]
        public PlcDataMsgType type;
        [MessagePackMember(1)]
        public PlcInputData inputs;
        [MessagePackMember(2)]
        public PlcOutputData outputs;
    }   
    public class PlcInputData
    {
        [MessagePackMember(0)]
        public List<byte> digIn;
        [MessagePackMember(1)]
        public List<byte> zoneBreach;
        [MessagePackMember(2)]
        public List<int> algIn;
        [MessagePackMember(3)]
        public int proxZoneStat;
    }    
    public class PlcOutputData
    {
        [MessagePackMember(0)]
        public List<byte> digOut;
        [MessagePackMember(1)]
        public List<byte> zoneActivate;
    }  
benlemond commented 5 years ago

Current attempt:

MessagePackObject mpo = MessagePackSerializer.UnpackMessagePackObject(e.ApplicationMessage.Payload);
MsgPackMsg mpm = MessagePackSerializer.Get<MsgPackMsg>().FromMessagePackObject(mpo);