コード例 #1
0
ファイル: Struct.cs プロジェクト: tealove/protobuf-net-1
        public void GenerateTypeSerializer()
        {
            var head = new TypeSerializer(typeof(CustomerStruct),
                                          new int[] { 1, 2 },
                                          new IProtoSerializer[] {
                new PropertyDecorator(typeof(CustomerStruct), typeof(CustomerStruct).GetProperty("Id"), new TagDecorator(1, WireType.Variant, false, new Int32Serializer())),
                new FieldDecorator(typeof(CustomerStruct), typeof(CustomerStruct).GetField("Name"), new TagDecorator(2, WireType.String, false, new StringSerializer()))
            }, null, false, true, null, null, null);
            var            ser   = CompilerContext.BuildSerializer(head);
            var            deser = CompilerContext.BuildDeserializer(head);
            CustomerStruct cs1   = new CustomerStruct {
                Id = 123, Name = "Fred"
            };

            using (MemoryStream ms = new MemoryStream())
            {
                using (ProtoWriter writer = new ProtoWriter(ms, null, null))
                {
                    ser(cs1, writer);
                }
                byte[] blob = ms.ToArray();
                ms.Position = 0;
                using (ProtoReader reader = new ProtoReader(ms, null, null))
                {
                    CustomerStruct?cst = (CustomerStruct?)deser(null, reader);
                    Assert.IsTrue(cst.HasValue);
                    CustomerStruct cs2 = cst.Value;
                    Assert.AreEqual(cs1.Id, cs2.Id);
                    Assert.AreEqual(cs1.Name, cs2.Name);
                }
            }
        }
コード例 #2
0
ファイル: Struct.cs プロジェクト: whut/protobuf-net
        public void RunStructDesrializerForEmptyStream()
        {
            var model = ProtoBuf.Meta.TypeModel.Create();
            var head  = new TypeSerializer(model, typeof(CustomerStruct),
                                           new int[] { 1, 2 },
                                           new IProtoSerializer[] {
                new PropertyDecorator(model, typeof(CustomerStruct), typeof(CustomerStruct).GetProperty("Id"), new TagDecorator(1, WireType.Variant, false, new Int32Serializer(model))),
                new FieldDecorator(typeof(CustomerStruct), typeof(CustomerStruct).GetField("Name"), new TagDecorator(2, WireType.String, false, new StringSerializer(model)))
            }, null, false, true, null, null, null);
            var deser = CompilerContext.BuildDeserializer(head, model);

            using (var reader = new ProtoReader(Stream.Null, null, null))
            {
                Assert.IsInstanceOfType(typeof(CustomerStruct), deser(null, reader));
            }
            using (var reader = new ProtoReader(Stream.Null, null, null))
            {
                CustomerStruct before = new CustomerStruct {
                    Id = 123, Name = "abc"
                };
                CustomerStruct after = (CustomerStruct)deser(before, reader);
                Assert.AreEqual(before.Id, after.Id);
                Assert.AreEqual(before.Name, after.Name);
            }
        }
コード例 #3
0
        public void GenerateTypeSerializer()
        {
            var model = ProtoBuf.Meta.TypeModel.Create();
            var head  = new TypeSerializer(typeof(CustomerStruct),
                                           new int[] { 1, 2 },
                                           new IProtoSerializer[] {
                new PropertyDecorator(typeof(CustomerStruct), typeof(CustomerStruct).GetProperty("Id"), new TagDecorator(1, WireType.Variant, false, PrimitiveSerializer <Int32Serializer> .Singleton)),
                new FieldDecorator(typeof(CustomerStruct), typeof(CustomerStruct).GetField("Name"), new TagDecorator(2, WireType.String, false, PrimitiveSerializer <StringSerializer> .Singleton))
            }, null, false, true, null, null, null);
            var            ser   = CompilerContext.BuildSerializer(head, model);
            var            deser = CompilerContext.BuildDeserializer(head, model);
            CustomerStruct cs1   = new CustomerStruct {
                Id = 123, Name = "Fred"
            };

            using (MemoryStream ms = new MemoryStream())
            {
                using (ProtoWriter writer = ProtoWriter.Create(out var state, ms, null, null))
                {
                    ser(writer, ref state, cs1);
                    writer.Close(ref state);
                }
                byte[] blob = ms.ToArray();
                ms.Position = 0;
                using (ProtoReader reader = ProtoReader.Create(out var state, ms, null, null))
                {
                    CustomerStruct?cst = (CustomerStruct?)deser(reader, ref state, null);
                    Assert.True(cst.HasValue);
                    CustomerStruct cs2 = cst.Value;
                    Assert.Equal(cs1.Id, cs2.Id);
                    Assert.Equal(cs1.Name, cs2.Name);
                }
            }
        }
コード例 #4
0
        public void RunStructDesrializerForEmptyStream()
        {
            var model = ProtoBuf.Meta.TypeModel.Create();
            var head  = new TypeSerializer(typeof(CustomerStruct),
                                           new int[] { 1, 2 },
                                           new IProtoSerializer[] {
                new PropertyDecorator(typeof(CustomerStruct), typeof(CustomerStruct).GetProperty("Id"), new TagDecorator(1, WireType.Variant, false, PrimitiveSerializer <Int32Serializer> .Singleton)),
                new FieldDecorator(typeof(CustomerStruct), typeof(CustomerStruct).GetField("Name"), new TagDecorator(2, WireType.String, false, PrimitiveSerializer <StringSerializer> .Singleton))
            }, null, false, true, null, null, null);
            var deser = CompilerContext.BuildDeserializer(head, model);

            using (var reader = ProtoReader.Create(out var state, Stream.Null, null, null))
            {
                Assert.IsType <CustomerStruct>(deser(reader, ref state, null));
            }
            using (var reader = ProtoReader.Create(out var state, Stream.Null, null, null))
            {
                CustomerStruct before = new CustomerStruct {
                    Id = 123, Name = "abc"
                };
                CustomerStruct after = (CustomerStruct)deser(reader, ref state, before);
                Assert.Equal(before.Id, after.Id);
                Assert.Equal(before.Name, after.Name);
            }
        }
コード例 #5
0
ファイル: Struct.cs プロジェクト: wuduanyu/protobuf-net
        public void GenerateTypeSerializer()
        {
            var model = ProtoBuf.Meta.RuntimeTypeModel.Create();
            var head  = TypeSerializer.Create(typeof(CustomerStruct),
                                              new int[] { 1, 2 },
                                              new IRuntimeProtoSerializerNode[] {
                new PropertyDecorator(typeof(CustomerStruct), typeof(CustomerStruct).GetProperty(nameof(CustomerStruct.Id)), new TagDecorator(1, WireType.Varint, false, Int32Serializer.Instance)),
                new FieldDecorator(typeof(CustomerStruct), typeof(CustomerStruct).GetField(nameof(CustomerStruct.Name)), new TagDecorator(2, WireType.String, false, StringSerializer.Instance))
            }, null, false, true, null, null, null, null, SerializerFeatures.WireTypeString | SerializerFeatures.CategoryMessage);
            var            ser   = CompilerContext.BuildSerializer <CustomerStruct>(model.Scope, head, model);
            var            deser = CompilerContext.BuildDeserializer <CustomerStruct>(model.Scope, head, model);
            CustomerStruct cs1   = new CustomerStruct {
                Id = 123, Name = "Fred"
            };

            using MemoryStream ms = new MemoryStream();
            var writeState = ProtoWriter.State.Create(ms, null, null);

            try
            {
                ser(ref writeState, cs1);
                writeState.Close();
            }
            finally
            {
                writeState.Dispose();
            }
            byte[] blob = ms.ToArray();
            ms.Position = 0;
            var state = ProtoReader.State.Create(ms, null, null);

            try
            {
                CustomerStruct?cst = (CustomerStruct?)deser(ref state, default);
                Assert.True(cst.HasValue);
                CustomerStruct cs2 = cst.Value;
                Assert.Equal(cs1.Id, cs2.Id);
                Assert.Equal(cs1.Name, cs2.Name);
            }
            finally
            {
                state.Dispose();
            }
        }
コード例 #6
0
ファイル: Struct.cs プロジェクト: wuduanyu/protobuf-net
        public void RunStructDesrializerForEmptyStream()
        {
            var model = ProtoBuf.Meta.RuntimeTypeModel.Create();
            var head  = TypeSerializer.Create(typeof(CustomerStruct),
                                              new int[] { 1, 2 },
                                              new IRuntimeProtoSerializerNode[] {
                new PropertyDecorator(typeof(CustomerStruct), typeof(CustomerStruct).GetProperty(nameof(CustomerStruct.Id)), new TagDecorator(1, WireType.Varint, false, Int32Serializer.Instance)),
                new FieldDecorator(typeof(CustomerStruct), typeof(CustomerStruct).GetField(nameof(CustomerStruct.Name)), new TagDecorator(2, WireType.String, false, StringSerializer.Instance))
            }, null, false, true, null, null, null, null, SerializerFeatures.WireTypeString | SerializerFeatures.CategoryMessage);
            var deser = CompilerContext.BuildDeserializer <CustomerStruct>(model.Scope, head, model);

            var state = ProtoReader.State.Create(Stream.Null, null, null);

            try
            {
                var result = deser(ref state, default);
                Assert.IsType <CustomerStruct>(result);
            }
            finally
            {
                state.Dispose();
            }

            state = ProtoReader.State.Create(Stream.Null, null, null);
            try
            {
                CustomerStruct before = new CustomerStruct {
                    Id = 123, Name = "abc"
                };
                CustomerStruct after = (CustomerStruct)deser(ref state, before);
                Assert.Equal(before.Id, after.Id);
                Assert.Equal(before.Name, after.Name);
            }
            finally
            {
                state.Dispose();
            }
        }
コード例 #7
0
 private CompiledSerializer(IProtoTypeSerializer head, TypeModel model)
 {
     this.head         = head;
     this.serializer   = CompilerContext.BuildSerializer(head, model);
     this.deserializer = CompilerContext.BuildDeserializer(head, model);
 }