Example #1
0
        private static RPositionsOld TranslateDictionary(Dictionary <object, object> data)
        {
            IEnumerable <(string Key, object Value)> IterateOver(KeyValuePair <object, object> item)
            {
                var key = string.Empty;

                if (item.Key is byte[] bytes) //pre python 3 it is byte array. can be removed post moving to 3
                {
                    key = Encoding.Default.GetString(bytes);
                }
                else if (item.Key is string value) //python 3 is string
                {
                    key = value;
                }

                object val = item.Value;

                if (val is Dictionary <object, object> dic)
                {
                    List <(string Key, object Value)> innerItems = new List <(string Key, object Value)>(0);
                    foreach (KeyValuePair <object, object> itm in dic)
                    {
                        innerItems.AddRange(IterateOver(itm).ToList());
                    }

                    yield return(key, innerItems);
                }
                else
                {
                    yield return(key, val);
                }
            }

            Dictionary <string, object> result = new Dictionary <string, object>();

            foreach (KeyValuePair <object, object> itm in data)
            {
                var converted = IterateOver(itm).ToList();
                foreach ((string key, object value) in converted)
                {
                    result.Add(key, value);
                }
            }

            RPositionsOld p = new RPositionsOld();

            p.Timestamp = (ulong)result["timestamp"];
            /////////////////////////////////////////////////////////
            p.Data = (result["nav"] as List <(string, object)>)
                     .Select(e => new RpositionsDataPoints(e.Item1, e.Item2 as object[])).ToList();
            return(p);
        }
Example #2
0
 public static RPositionsMessagePack Deserialize(byte[] data)
 {
     try
     {
         return(MessagePackSerializer.Deserialize <RPositionsMessagePack>(data));
     }
     catch (Exception e)
     {
         string path = $@"RPosition_Error_{DateTime.Now.Ticks}.bin";
         File.WriteAllBytes(path, data);
         var fallback = RPositionsOld.Deserialize(data);
         RPositionsMessagePack msg = new RPositionsMessagePack();
         msg.Timestamp      = fallback.Timestamp;
         msg.NavigationData = new List <NavigationData>(fallback.Data.Count);
         foreach (var rpos in fallback.Data)
         {
             msg.NavigationData.Add(new NavigationData {
                 Name = rpos.Name, Points = rpos.Points.ToList()
             });
         }
         return(msg);
     }
 }