Пример #1
0
        public static Packet unPack(string s)
        {
            int    a      = s.Length - s.Replace("{", "").Replace("[", "").Length;
            int    b      = s.Length - s.Replace("}", "").Replace("]", "").Length;
            string checkS = s.Replace(Environment.NewLine, "").Trim();

            if (a != b || !checkS.Substring(0, 1).Equals("{") || !checkS.Substring(checkS.Length - 3, 1).Equals("}") ||
                !checkS.Contains("flavour") || !checkS.Contains("key"))
            {
                Console.WriteLine("Packet: invalid string");
                Console.WriteLine(s);
                Console.WriteLine("-- end print --");
                Console.WriteLine(checkS.Substring(checkS.Length - 2, 1).ToCharArray()[0]);
                //Console.WriteLine(checkS.Substring(checkS.Length - 3, 1));
                //Console.WriteLine("a: {0}, b: {1}", a, b);
                throw new InvalidDataException();
            }

            Flavour   f;
            PacketKey k = new PacketKey();
            ComData   d = new ComData();

            JSONDecoder jd = new JSONDecoder(s);

            jd.beginItem();

            string flav = jd.getField()[1].Trim();

            f = (Flavour)Enum.Parse(typeof(Flavour), flav);

            jd.getField();  // key
            k.fillFromJSON(jd);

            if (f != Flavour.ECHO)
            {
                jd.getField();  // data
                d.fillFromJSON(jd);
            }

            jd.endItem();

            return(new Packet(f, k, d));
        }
Пример #2
0
 public void fillFromJSON(JSONDecoder jd)
 {
     jd.beginItem();
     value = long.Parse(jd.getField()[1]);
     jd.endItem();
 }