static void TestPrimitives() { Biser.Encoder en = new Biser.Encoder(); DateTime dt = DateTime.UtcNow; decimal d1 = 548.45m; string s1 = "well done"; bool? b1 = null; double o1 = 45.7887; //Adding one by one to encoder 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); //decoder is based on encoded byte[] //Decoding one by one in the same sequence dt = dec.GetDateTime(); d1 = dec.GetDecimal(); s1 = dec.GetString(); b1 = dec.GetBool_NULL(); o1 = dec.GetDouble(); }
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 }
static void TestListDictionary() { //Encoding Biser.Encoder enc = new Biser.Encoder(); enc.Add((int)123); enc.Add(new List <string> { "Hi", "there" }, r => { enc.Add(r); }); enc.Add((float)-458.4f); enc.Add(new Dictionary <uint, string> { { 1, "Well" }, { 2, "done" } } , r => { enc.Add(r.Key); enc.Add(r.Value); }); enc.Add((decimal) - 587.7m); //TS4 implements IEncoder enc.Add(new Dictionary <uint, TS4> { { 1, new TS4 { TermId = 1 } }, { 2, new TS4 { TermId = 5 } } } , r => { enc.Add(r.Key); enc.Add(r.Value); }); enc.Add(new TS4 { TermId = 188 }); //Decoding var decoder = new Biser.Decoder(enc.Encode()); Console.WriteLine(decoder.GetInt()); //////Alternative to the following instruction. Slower than supplying List directly ////foreach (var item in decoder.GetCollection().Select(r => r.GetString())) ////{ //// Console.WriteLine(item); ////} List <string> lst = decoder.CheckNull() ? null : new List <string>(); if (lst != null) { decoder.GetCollection(() => { return(decoder.GetString()); }, lst, true); foreach (var item in lst) { Console.WriteLine(item); } } Console.WriteLine(decoder.GetFloat()); ////////Alternative to the following instruction. Slower than supplying Dictionary directly //////foreach (var item in decoder.GetCollection()) //////{ ////// Console.WriteLine($"K: {item.GetUInt()}; V: {item.GetString()}"); //////} Dictionary <uint, string> d1 = decoder.CheckNull() ? null : new Dictionary <uint, string>(); if (d1 != null) { decoder.GetCollection( () => { return(decoder.GetUInt()); }, () => { return(decoder.GetString()); }, d1, true); foreach (var item in d1) { Console.WriteLine(item.Key + "; " + item.Value); } } Console.WriteLine(decoder.GetDecimal()); Dictionary <uint, TS4> d2 = decoder.CheckNull() ? null : new Dictionary <uint, TS4>(); if (d2 != null) { decoder.GetCollection( () => { return(decoder.GetUInt()); }, () => { return(TS4.BiserDecode(extDecoder: decoder)); }, d2, true); foreach (var item in d2) { Console.WriteLine(item.Key + "; " + item.Value.TermId); } } Console.WriteLine(TS4.BiserDecode(extDecoder: decoder).TermId); }