Example #1
0
        private static byte[] UseJsonToolForValidation <T>(T value, BssomSerializerOptions option = null)
        {
            var buf = BssomSerializer.Serialize(value, option);

            BssomSerializer.Size(value, option).Is(buf.Length);
            var t = BssomSerializer.Deserialize <T>(buf, option);

            Kooboo.Json.JsonSerializer.ToJson(t).Is(Kooboo.Json.JsonSerializer.ToJson(value));
            return(buf);
        }
Example #2
0
        public static void VerifySpecific(StringDictionary value, BssomSerializerOptions option = null)
        {
            var buf = BssomSerializer.Serialize(value, option);

            BssomSerializer.Size(value, option).Is(buf.Length);
            var dict  = new Dictionary <string, string>(BssomSerializer.Deserialize <StringDictionary>(buf, option).ToIEnumerable().ToIDict());
            var dict2 = new Dictionary <string, string>(value.ToIEnumerable().ToIDict());

            dict.IsDict(dict2);
        }
Example #3
0
        public static void VerifySpecific <T>(IGrouping <T, T> value, BssomSerializerOptions option = null)
        {
            var buf = BssomSerializer.Serialize(value, option);

            BssomSerializer.Size(value, option).Is(buf.Length);
            var group = BssomSerializer.Deserialize <IGrouping <T, T> >(buf, option);

            group.Key.Is(value.Key);
            group.Is(value);
        }
Example #4
0
        public void DeserializeTestWithStream_Type_Context()
        {
            int val     = int.MaxValue;
            var context = new BssomDeserializeContext();
            var buf     = BssomSerializer.Serialize(val);
            var stream  = new FakePipeStream(buf);

            ((int)BssomSerializer.Deserialize(ref context, stream, typeof(int))).Is(int.MaxValue);
            stream.CurrentCursor().Is(5);
        }
Example #5
0
        public void SerializeTestWithStreamAsync()
        {
            int val    = int.MaxValue;
            var stream = new FakePipeStream();

            BssomSerializer.SerializeAsync(stream, val).Wait();
            stream.CurrentCursor().Is(5);
            BssomSerializer.Deserialize <int>(stream.ToArray(), 0, out int readSize).Is(int.MaxValue);
            readSize.Is(5);
        }
Example #6
0
        public void SerializeTestWithContext()
        {
            int val     = int.MaxValue;
            var context = new BssomSerializeContext();
            var buf     = BssomSerializer.Serialize(ref context, val);

            buf.Length.Is(5);
            BssomSerializer.Deserialize <int>(buf, 0, out int readSize).Is(int.MaxValue);
            readSize.Is(5);
        }
Example #7
0
        public static void ConvertObjectAndVerifyEntity(object value, BssomSerializerOptions option = null)
        {
            var buf = BssomSerializer.Serialize(value, option);

            BssomSerializer.Size(value, option).Is(buf.Length);
            buf.IsMap2();
            var map = (BssomMap)BssomSerializer.Deserialize <object>(buf, option);

            value.GetPublicMembersWithDynamicObject().IsMap(map);
        }
Example #8
0
        public void ClassType_DefaultPopulateConstructParameters_Test()
        {
            var val2 = new _Attribute_Class_2(0);

            val2.Orgin.Is(4);

            var buf  = BssomSerializer.Serialize(val2);
            var val3 = BssomSerializer.Deserialize <_Attribute_Class_2>(buf);

            val3.Orgin.Is(4);
        }
Example #9
0
        public void SerializeTestWithContext_Stream()
        {
            int val     = int.MaxValue;
            var context = new BssomSerializeContext();
            var stream  = new FakePipeStream();

            BssomSerializer.Serialize(ref context, val, stream);
            stream.CurrentCursor().Is(5);
            BssomSerializer.Deserialize <int>(stream.ToArray(), 0, out int readSize).Is(int.MaxValue);
            readSize.Is(5);
        }
Example #10
0
        public void MemoryStreamDeserializeTest(Type type)
        {
            var obj = RandomHelper.RandomValueWithOutStringEmpty(type);

            byte[] buf = BssomSerializer.Serialize(obj);

            MemoryStream stream = new MemoryStream(buf);

            obj = BssomSerializer.Deserialize(stream, type);
            buf.Is(BssomSerializer.Serialize(obj));
        }
Example #11
0
        public object Deserialize(byte[] bytes, Type type)
        {
            var result = BssomSerializer.Deserialize(bytes, 0, out var readSize, type);

            if (readSize != bytes.Length)
            {
                throw new BssomSerializationException($"Deserializing {type.FullName} has a error, expected buffer size: {bytes.Length}, actual buffer size: {readSize}.");
            }

            return(result);
        }
Example #12
0
        public void Map1Deserialize_NonField_FormatterIsCorrectly()
        {
            Dictionary <string, int> dic = new Dictionary <string, int>();

            dic.Add("A12345678", 1);
            dic.Add("B12345678", 2);
            dic.Add("C12345678", 3);

            var buf = BssomSerializer.Serialize(dic);
            var c1  = BssomSerializer.Deserialize <_Map1_4_1>(buf);
        }
        public void MapCodeGenOnlySerializePublicElement()
        {
            _PrivateSet w = new _PrivateSet();

            w.Assgin(1, 3, true);
            var buf = BssomSerializer.Serialize(w);
            var w2  = BssomSerializer.Deserialize <_PrivateSet>(buf);

            w2.A.Is(0);
            w2.B.Is(3);
            w2.C.IsFalse();
        }
Example #14
0
        public void SerializeTestWithRefBytes_Expanded()
        {
            int val = int.MaxValue;

            byte[] buf = new byte[3];
            buf[1] = 3;
            BssomSerializer.Serialize(ref buf, 2, val).Is(5);
            buf.Length.Is(7);
            buf[1].Is(3);
            BssomSerializer.Deserialize <int>(buf, 2, out int readSize).Is(int.MaxValue);
            readSize.Is(5);
        }
Example #15
0
        public void SerializeTestWithIBssomBufferWriter()
        {
            int val  = int.MaxValue;
            var bufw = ExpandableBufferWriter.CreateGlobar();

            BssomSerializer.Serialize(val, bufw);
            var buf = bufw.GetBufferedArray();

            buf.Length.Is(5);
            BssomSerializer.Deserialize <int>(buf, 0, out int readSize).Is(int.MaxValue);
            readSize.Is(5);
        }
Example #16
0
        public void Map1Deserialize_OneZeroAndOneNotZero()
        {
            Dictionary <string, int> dic = new Dictionary <string, int>();

            dic.Add("A", 1);
            dic.Add("B12345678", 2);
            var buf = BssomSerializer.Serialize(dic);
            var c1  = BssomSerializer.Deserialize <_Map1_6>(buf);

            c1.A.Is(1);
            c1.B12345678.Is(2);
        }
Example #17
0
        public static void VerifySpecific <T>(ILookup <T, T> value, BssomSerializerOptions option = null)
        {
            var buf = BssomSerializer.Serialize(value, option);

            BssomSerializer.Size(value, option).Is(buf.Length);
            var val = BssomSerializer.Deserialize <ILookup <T, T> >(buf, option);

            val.Count.Is(value.Count);
            foreach (var item in value)
            {
                VerifySpecific(item, option);
            }
        }
Example #18
0
        public void SerializeTestWithRefBytes_Context()
        {
            BssomSerializeContext context = new BssomSerializeContext();
            int val = int.MaxValue;

            byte[] buf = new byte[8];
            buf[1] = 3;
            BssomSerializer.Serialize(ref context, ref buf, 2, val).Is(5);
            buf.Length.Is(8);
            buf[1].Is(3);
            BssomSerializer.Deserialize <int>(buf, 2, out int readSize).Is(int.MaxValue);
            readSize.Is(5);
        }
Example #19
0
        public void FieldTypeFormatterAttribute_Map1Deserialize_Test()
        {
            Dictionary <string, object> dict = new Dictionary <string, object>()
            {
                { "Name", "a" },
                { "Age", 10 }
            };
            var buf = BssomSerializer.Serialize(dict);
            var t   = BssomSerializer.Deserialize <_Attribute_Class_4>(buf);

            t.Name.Is("a");
            t.Age.Is(11);
        }
Example #20
0
        public void ReadByIgnoringBlankCharactersTest(object val, Func <object, bool> test)
        {
            var obj = new byte[3000];
            var buf = BssomSerializer.Serialize(obj);

            var bsfw = new BssomFieldMarshaller(buf);

            bsfw.TryWrite(BssomFieldOffsetInfo.Zero, val).IsTrue();
            test(BssomSerializer.Deserialize(buf, 0, out int readSize, val.GetType())).IsTrue();

            bsfw.TryWrite(BssomFieldOffsetInfo.Zero, val).IsTrue();
            test(BssomSerializer.Deserialize(buf, 0, out readSize, val.GetType())).IsTrue();
        }
Example #21
0
        public void Map1Deserialize_AtSameLayer_IfNotEqualThenSKip()
        {
            Dictionary <string, int> dic = new Dictionary <string, int>();

            dic.Add("A1", 1);
            dic.Add("A2", 2);

            var buf = BssomSerializer.Serialize(dic);
            var c1  = BssomSerializer.Deserialize <_Map1_1>(buf);

            c1.A1.Is(1);
            c1.A3.Is(0);
        }
Example #22
0
        public void Map2Deserialize_TryGetSlowLogicIsCorrectly()
        {
            var val = RandomHelper.RandomValue <_Class_1>();
            var buf = BssomSerializer.Serialize(val);
            var c1  = BssomSerializer.Deserialize <_Class_1>(new SlowBssomBuffer(buf));

            val.A1.Is(c1.A1);
            val.B1.Is(c1.B1);
            val.C1.Is(c1.C1);
            val.D1.Is(c1.D1);
            val._1234567A.Is(c1._1234567A);
            val._2234567B.Is(c1._2234567B);
            val._3234567C.Is(c1._3234567C);
        }
Example #23
0
        public void Map2Deserialize_EnumeratorSlowIsCorrectly()
        {
            var val = RandomHelper.RandomValue <_Class_1>();
            var buf = BssomSerializer.Serialize(val);
            var c1  = BssomSerializer.Deserialize <Dictionary <string, object> >(new SlowBssomBuffer(buf));

            val.A1.Is(c1["A1"]);
            val.B1.Is(c1["B1"]);
            val.C1.Is(c1["C1"]);
            val.D1.Is(c1["D1"]);
            val._1234567A.Is(c1["_1234567A"]);
            val._2234567B.Is(c1["_2234567B"]);
            val._3234567C.Is(c1["_3234567C"]);
        }
Example #24
0
        public void Array2_ReadStrongType_FunctionIsCorrectly()
        {
            var val = RandomHelper.RandomValue <List <string> >();
            var buf = BssomSerializer.Serialize(val);
            var r   = BssomSerializer.Deserialize <List <string> >(buf);

            var bsfm  = new BssomFieldMarshaller(buf);
            int count = ((ICollection)val).Count;

            for (int i = 0; i < count; i++)
            {
                bsfm.ReadValue <string>(bsfm.IndexOf($"${i}")).Is(val[i]);
            }
        }
        public void Map1Deserialize_CompositedResolverAllowPrivateTest()
        {
            var val = new Dictionary <string, int>()
            {
                { "A1", 1 },
                { "B1", 2 },
                { "C1", 3 },
                { "D1", 4 },
                { "E1", 5 },
            };
            var buf  = BssomSerializer.Serialize(val);
            var val2 = BssomSerializer.Deserialize <_PrivateMembersClass>(buf, BssomSerializerOptions.DefaultAllowPrivate);

            _PrivateMembersClass.Create(1, 2, 3, 4, 5).Equals(val2).IsTrue();
        }
Example #26
0
        public static BssomArray ConvertArray3ObjectAndVerifyEntity <T>(T value, BssomSerializerOptions option = null)
        {
            if (option == null)
            {
                option = BssomSerializerOptions.IntKeyCompositedResolverOption;
            }
            var buf = BssomSerializer.Serialize(value, option);

            BssomSerializer.Size(value, option).Is(buf.Length);
            buf.IsArray3();
            var desObj = BssomSerializer.Deserialize <T>(buf, option);

            Kooboo.Json.JsonSerializer.ToJson(value).Is(Kooboo.Json.JsonSerializer.ToJson(desObj));
            return(BssomSerializer.Deserialize <BssomArray>(buf, option));
        }
Example #27
0
        public void Map1Deserialize_KeyIsNotFirstLayer()
        {
            Dictionary <string, int> dic = new Dictionary <string, int>();

            dic.Add("A12345678", 1);
            dic.Add("B12345678", 2);
            dic.Add("C12345678", 3);

            var buf = BssomSerializer.Serialize(dic);
            var c1  = BssomSerializer.Deserialize <_Map1_5>(buf);

            c1.A12345678.Is(1);
            c1.B12345678.Is(2);
            c1.C12345678.Is(3);
        }
Example #28
0
        public void Map_WriteFieldWithBlankLength_IsExtraLengthWriting_FixTypeSize_IsCorrectly()
        {
            var val = RandomHelper.RandomValue <_class2>();
            var buf = BssomSerializer.Serialize(val);

            var bsfm = new BssomFieldMarshaller(buf);


            int B1 = RandomHelper.RandomValue <int>();

            bsfm.TryWrite(bsfm.IndexOf("[" + nameof(_class2.B) + "]"), B1).IsTrue();

            var val3 = BssomSerializer.Deserialize <BssomMap>(buf);

            val3["B"].Is(BssomValue.Create(B1));
            bsfm.ReadValue <int>(bsfm.IndexOf("[" + nameof(_class2.B) + "]")).Is(B1);
        }
Example #29
0
        public void Map1Deserialize_KeyIsNotNextLayer()
        {
            Dictionary <string, int> dic = new Dictionary <string, int>();

            dic.Add("A123", 1);
            dic.Add("B123", 2);
            dic.Add("C123", 3);
            dic.Add("D123", 4);

            var buf = BssomSerializer.Serialize(dic);
            var c1  = BssomSerializer.Deserialize <_Map1_4>(buf);

            c1.A123.Is(1);
            c1.B123.Is(2);
            c1.C123.Is(3);
            c1.D123.Is(4);
        }
Example #30
0
        public void Map1Deserialize_MultiNotSameLayer_IfNotEqualThenSKip()
        {
            Dictionary <string, int> dic = new Dictionary <string, int>();

            dic.Add("A1234567", 1);
            dic.Add("A12345678", 2);
            dic.Add("B1234567", 3);
            dic.Add("B12345678", 4);

            var buf = BssomSerializer.Serialize(dic);
            var c1  = BssomSerializer.Deserialize <_Map1_3>(buf);

            c1.A1234567.Is(1);
            c1.A12345679.Is(0);
            c1.B1234567.Is(3);
            c1.B12345679.Is(0);
        }