Пример #1
0
        public void TestSimplePropertyPackUnpack()
        {
            using (IBuffer buffer = new SimpleBuffer())
            {
                RawDataSerializer ms = new RawDataSerializer(buffer);
                BinaryPackager packer = new BinaryPackager();

                RegisteredPackageDefinition registeredPackageDefinition;
                packer.RegisterDefinition(new PackageDefinition<PackData>()
                                                        .Define(x => x.IntData, x => x.IntData)
                                                        .Define(x => x.StringData, x => x.StringData)
                                                        .Define(x => x.DateTimeData, x => x.DateTimeData), out registeredPackageDefinition);
                var packData = new PackData()
                {
                    ArrayPackData = null,
                    DateTimeData = DateTime.Now,
                    IntData = 124,
                    StringData = "Some String",
                };

                packer.Pack(packData, ms);

                using (StreamDeserilizer deserilizer = new StreamDeserilizer(buffer.Buffer))
                {
                    Unpacker unpacker = new Unpacker();
                    unpacker.AddPackageDefinition(new EmptyConstractorUnpackageDefinition<PackData>()
                    {
                        DefinitionId = registeredPackageDefinition.DefinitionId
                    }
                                                      .DefineInt("IntData", (data, i) => data.IntData = i)
                                                      .DefineString("StringData", (data, s) => data.StringData = s)
                                                      .DefineDateTime("DateTimeData",
                                                                      (data, time) => data.DateTimeData = time));

                    var unpackData = unpacker.Unpack<PackData>(deserilizer);
                    Assert.IsNotNull(unpackData);
                    CheckValidPackData(packData, unpackData);
                    Assert.AreEqual(packData.ArrayPackData, unpackData.ArrayPackData);
                }
            }
        }
Пример #2
0
        public void TestArrayWithSimplePropertyPackUnpack()
        {
            using (IBuffer buffer = new SimpleBuffer())
            {
                RawDataSerializer ms = new RawDataSerializer(buffer);
                BinaryPackager packer = new BinaryPackager();

                RegisteredPackageDefinition registeredPackageDefinition;
                packer.RegisterDefinition(new PackageDefinition<PackData>()
                                                        .Define(x => x.IntData, x => x.IntData)
                                                        .Define(x => x.StringData, x => x.StringData)
                                                        .Define(x => x.ArrayPackData, x => x.ArrayPackData)
                                                        .Define(x => x.DateTimeData, x => x.DateTimeData), out registeredPackageDefinition);
                var packData = new PackData()
                {
                    ArrayPackData = new List<PackData>()
                                        {
                                            new PackData()
                                            {
                                                IntData =1,
                                                StringData = "First String",
                                            },
                                            new PackData()
                                            {
                                                IntData =2,
                                                DateTimeData = DateTime.Now + TimeSpan.FromHours(2),
                                                ArrayPackData =  new List<PackData>(),
                                            },
                                            new PackData()
                                            {
                                                StringData = string.Empty
                                            },
                                        },
                    DateTimeData = DateTime.Now,
                    IntData = 124,
                    StringData = "Some String",
                };

                packer.Pack(packData, ms);

                using (StreamDeserilizer deserilizer = new StreamDeserilizer(buffer.Buffer))
                {

                    Unpacker unpacker = new Unpacker();
                    unpacker.AddPackageDefinition(new EmptyConstractorUnpackageDefinition<PackData>()
                    {
                        DefinitionId = registeredPackageDefinition.DefinitionId
                    }
                                                      .DefineInt("IntData", (data, i) => data.IntData = i)
                                                      .DefineString("StringData", (data, s) => data.StringData = s)
                                                      .DefineEnumerable("ArrayPackData",
                                                                        (data, enumerable) => data.ArrayPackData = enumerable.NullGurd(x => x.OfType<PackData>().ToList()))
                                                      .DefineDateTime("DateTimeData", (data, time) => data.DateTimeData = time));

                    var unpackData = unpacker.Unpack<PackData>(deserilizer);
                    Assert.IsNotNull(unpackData);
                    CheckValidPackData(unpackData, packData);
                    Assert.IsNotNull(packData.ArrayPackData);

                    Assert.AreEqual(3, packData.ArrayPackData.Count);

                    CheckValidPackData(packData.ArrayPackData[0], unpackData.ArrayPackData[0]);
                    Assert.IsNull(unpackData.ArrayPackData[0].ArrayPackData);

                    CheckValidPackData(packData.ArrayPackData[1], unpackData.ArrayPackData[1]);
                    Assert.IsNotNull(unpackData.ArrayPackData[1].ArrayPackData);
                    Assert.AreEqual(0, unpackData.ArrayPackData[1].ArrayPackData.Count);

                    CheckValidPackData(packData.ArrayPackData[2], unpackData.ArrayPackData[2]);
                    Assert.IsNull(unpackData.ArrayPackData[2].ArrayPackData);
                }
            }
        }
Пример #3
0
        public void TestInstanceCreatorPackUnpack()
        {
            using (IBuffer buffer = new SimpleBuffer())
            {
                RawDataSerializer ms = new RawDataSerializer(buffer);
                BinaryPackager packer = new BinaryPackager();

                RegisteredPackageDefinition registeredPackageDefinition;
                packer.RegisterDefinition(new PackageDefinition<PackData>(), out registeredPackageDefinition);

                packer.Pack(new PackData(), ms);

                using (StreamDeserilizer deserilizer = new StreamDeserilizer(buffer.Buffer))
                {
                    Unpacker unpacker = new Unpacker();
                    unpacker.AddPackageDefinition(new EmptyConstractorUnpackageDefinition<PackData>() { DefinitionId = registeredPackageDefinition.DefinitionId });

                    Assert.IsNotNull(unpacker.Unpack<PackData>(deserilizer));
                }
            }
        }