Пример #1
0
        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
        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);
                }
            }
        }
Пример #3
0
        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();
            }
        }
Пример #4
0
 private CompiledSerializer(IProtoTypeSerializer head, TypeModel model)
 {
     this.head         = head;
     this.serializer   = CompilerContext.BuildSerializer(head, model);
     this.deserializer = CompilerContext.BuildDeserializer(head, model);
 }