Beispiel #1
0
        public void CanSerializeViaInputOutputAPI()
        {
            using var ms = new MemoryStream();
            IProtoOutput <Stream> output = RuntimeTypeModel.Default;
            var orig = new SomeModel {
                Id = 42
            };

            output.Serialize(ms, orig);

            ms.Position = 0;

            IProtoInput <Stream> input = RuntimeTypeModel.Default;
            var clone = input.Deserialize <SomeModel>(ms);

            Assert.NotSame(orig, clone);
            Assert.Equal(42, clone.Id);

            IProtoInput <byte[]> arrayInput = RuntimeTypeModel.Default;

            clone = arrayInput.Deserialize <SomeModel>(ms.ToArray());

            Assert.NotSame(orig, clone);
            Assert.Equal(42, clone.Id);

            var segment = new ArraySegment <byte>(ms.GetBuffer(), 0, (int)ms.Length);
            IProtoInput <ArraySegment <byte> > segmentInput = RuntimeTypeModel.Default;

            clone = segmentInput.Deserialize <SomeModel>(segment);

            Assert.NotSame(orig, clone);
            Assert.Equal(42, clone.Id);
        }
Beispiel #2
0
        public void CanSerializeViaInputOutputAPI_Buffers()
        {
            using var buffer = BufferWriter <byte> .Create();

            IProtoOutput <IBufferWriter <byte> > output = RuntimeTypeModel.Default;

            var orig = new SomeModel {
                Id = 42
            };

            output.Serialize(buffer, orig);

            using var payload = buffer.Flush();
            IProtoInput <ReadOnlySequence <byte> > input = RuntimeTypeModel.Default;
            var clone = input.Deserialize <SomeModel>(payload.Value);

            Assert.NotSame(orig, clone);
            Assert.Equal(42, clone.Id);
        }