Example #1
0
        public static TS1 BiserDecode(byte[] enc = null, Biser.Decoder extDecoder = null) //!!!!!!!!!!!!!! change return type
        {
            Biser.Decoder decoder = null;
            if (extDecoder == null)
            {
                if (enc == null || enc.Length == 0)
                {
                    return(null);
                }
                decoder = new Biser.Decoder(enc);
                if (decoder.CheckNull())
                {
                    return(null);
                }
            }
            else
            {
                if (extDecoder.CheckNull())
                {
                    return(null);
                }
                else
                {
                    decoder = extDecoder;
                }

                //decoder = new Biser.Decoder(extDecoder);
                //if (decoder.IsNull)
                //    return null;
            }

            TS1 m = new TS1();  //!!!!!!!!!!!!!! change return type

            m.P1 = decoder.GetInt();
            m.P2 = decoder.GetInt();
            m.P3 = decoder.GetDecimal();

            m.P4 = decoder.CheckNull() ? null : new List <TS2>();
            if (m.P4 != null)
            {
                decoder.GetCollection(() => { return(TS2.BiserDecode(null, decoder)); }, m.P4, true);
            }

            m.P5 = decoder.CheckNull() ? null : new Dictionary <long, TS3>();
            if (m.P5 != null)
            {
                decoder.GetCollection(() => {
                    return(decoder.GetLong());
                },
                                      () => { return(TS3.BiserDecode(null, decoder)); }, m.P5, true);
            }

            m.P6 = decoder.CheckNull() ? null : new Dictionary <uint, List <TS3> >();
            if (m.P6 != null)
            {
                decoder.GetCollection(
                    () => { return(decoder.GetUInt()); },
                    () =>
                {
                    var iList = decoder.CheckNull() ? null : new List <TS3>();
                    if (iList != null)
                    {
                        decoder.GetCollection(() => { return(TS3.BiserDecode(extDecoder: decoder)); }, iList, true);
                    }
                    return(iList);
                },
                    m.P6, true);
            }

            m.P7 = TS2.BiserDecode(extDecoder: decoder);

            m.P8 = decoder.CheckNull() ? null : new List <Tuple <string, byte[], TS3> >();
            if (m.P8 != null)
            {
                decoder.GetCollection
                    (() => {
                    return(new Tuple <string, byte[], TS3>
                               (decoder.GetString(),
                               decoder.GetByteArray(),
                               TS3.BiserDecode(null, decoder)));
                }, m.P8, true);
            }

            m.P9 = new Tuple <float, TS2, TS3, decimal?>
                       (decoder.GetFloat(), TS2.BiserDecode(null, decoder), TS3.BiserDecode(null, decoder), decoder.GetDecimal_NULL());

            return(m);
        }
Example #2
0
        static void TestCustom()
        {
            Biser.Encoder en = new Biser.Encoder();
            List <int>    l1 = new List <int> {
                1, 2, 3
            };
            List <TS2> l2 = new List <TS2> {
                new TS2 {
                    P1 = 15
                }, new TS2 {
                    P1 = 16
                }, new TS2 {
                    P1 = 17
                }
            };
            DateTime dt = DateTime.UtcNow;
            decimal  d1 = 548.45m;
            string   s1 = "well done";
            bool?    b1 = null;
            double   o1 = 45.7887;

            //It is possible to add IList directly, but we test custom serialization

            //Skipping saving NULLS
            //Addin length of the list l1
            en.Add(l1.Count);
            //Adding items one by one
            foreach (var item in l1)
            {
                en.Add(item);
            }
            //Addin length of the list l2
            en.Add(l2.Count);
            //Adding items one by one
            foreach (var item in l2)
            {
                en.Add(item); //item is IEncoder so can be easily added
            }
            //adding other elements
            en.Add(dt);
            en.Add(d1);
            en.Add(s1);
            en.Add(b1);
            en.Add(o1);

            byte[] btEnc = en.Encode(); //Getting serialized value

            //Decoding
            Biser.Decoder dec = new Biser.Decoder(btEnc);

            //No null checks for lists (we didn't save them)
            //Getting list l1
            l1 = new List <int>();

            var cnt = dec.GetInt();       //getting count of items - !!!Note!!! don'T put it into for-loop

            for (int i = 0; i < cnt; i++) //reading count of elements
            {
                l1.Add(dec.GetInt());     //getting items one by one
            }
            //Getting list l2
            l2  = new List <TS2>();
            cnt = dec.GetInt(); //getting count of items
            for (int i = 0; i < cnt; i++)
            {
                //getting items one by one, supplying existing decoder
                //to unroll TS2 element
                l2.Add(TS2.BiserDecode(extDecoder: dec));
            }

            //Getting other elements
            dt = dec.GetDateTime();
            d1 = dec.GetDecimal();
            s1 = dec.GetString();
            b1 = dec.GetBool_NULL();
            o1 = dec.GetDouble();

            //done
        }