private static Person DeserializePerson(PbfBlock data)
        {
            var result = new Person()
            {
                Phones = new List <PhoneNumber>(2)
            };

            var(fieldNumber, wireType) = data.ReadFieldHeader();
            while (fieldNumber != 0)
            {
                switch (fieldNumber)
                {
                case 1: result.Name = data.ReadString(); break;

                case 2: result.ID = data.ReadInt(); break;

                case 3: result.Email = data.ReadString(); break;

                case 4: result.Phones.Add(DeserializePhone(PbfBlock.Create(data.ReadLengthPrefixedBytes()))); break;

                default: data.SkipField(wireType); break;
                }

                (fieldNumber, wireType) = data.ReadFieldHeader();
            }

            return(result);
        }
        public void ReadVarint64_ReadsNumbers(byte[] data, ulong expectedNumber)
        {
            var block = PbfBlock.Create(data);

            var number = block.ReadVarint64();

            Assert.Equal(expectedNumber, number);
        }
        public void ReadLengthPrefixedBytes_ReadsData()
        {
            var block = PbfBlock.Create(new byte[] { 0x03, 0x41, 0x42, 0x43 });

            var data = block.ReadLengthPrefixedBytes();

            Assert.Equal(new byte[] { 0x41, 0x42, 0x43 }, data.ToArray());
        }
        public void ReadFixed32_ReadsNumbers(byte[] data, uint expectedNumber)
        {
            var block = PbfBlock.Create(data);

            var number = block.ReadFixed32();

            Assert.Equal(expectedNumber, number);
        }
Example #5
0
        public void ReadFieldHeader_ReadsFieldNumbers(byte[] data, int fieldNumber)
        {
            var block = PbfBlock.Create(data);

            var header = block.ReadFieldHeader();

            Assert.Equal(fieldNumber, header.fieldNumber);
        }
Example #6
0
        public void SkipFields_SkipsCorrectNumberOfBytes(byte[] data, WireType wireType, int expectedPosition)
        {
            var block = PbfBlock.Create(data);

            block.SkipField(wireType);

            Assert.Equal(expectedPosition, block.Position);
        }
Example #7
0
        public void ReadFieldHeader_ReadsWireTypes(byte[] data, WireType wireType)
        {
            var block = PbfBlock.Create(data);

            var header = block.ReadFieldHeader();

            Assert.Equal(wireType, header.wireType);
        }
Example #8
0
        private void ReadSingle_ReadsValues(byte[] data, float expectedNumber)
        {
            var block = PbfBlock.Create(data);

            var number = block.ReadSingle();

            Assert.Equal(expectedNumber, number);
        }
Example #9
0
        private void ReadLong_ReadsVarintValues(byte[] data, long expectedNumber)
        {
            var block = PbfBlock.Create(data);

            var number = block.ReadLong();

            Assert.Equal(expectedNumber, number);
        }
Example #10
0
        private void ReadSignedInt_ReadsZiggedVarintValues(byte[] data, int expectedNumber)
        {
            var block = PbfBlock.Create(data);

            var number = block.ReadSignedInt();

            Assert.Equal(expectedNumber, number);
        }
Example #11
0
        public void ReadBoolean_ReadsValuesEncodedAsVarint(byte[] data, bool expectedValue)
        {
            var block = PbfBlock.Create(data);

            var value = block.ReadBoolean();

            Assert.Equal(expectedValue, value);
        }
Example #12
0
        public void ReadString_ReadsUtf8EncodedString(byte[] data, string expectedText)
        {
            var block = PbfBlock.Create(data);

            var text = block.ReadString();

            Assert.Equal(expectedText, text);
        }
Example #13
0
        public void ReadFieldHeader_ReturnsNoneWhenEndOfBlockIsReached()
        {
            var block = PbfBlock.Create(new byte[] { });

            var header = block.ReadFieldHeader();

            Assert.Equal(0, header.fieldNumber);
            Assert.Equal(WireType.None, header.wireType);
        }
        public static AddressBook Deserialize(byte[] source)
        {
            var data = PbfBlock.Create(source);

            var result = new AddressBook()
            {
                People = new List <Person>(10000)
            };

            var(fieldNumber, wireType) = data.ReadFieldHeader();
            while (fieldNumber != 0)
            {
                switch (fieldNumber)
                {
                case 1: result.People.Add(DeserializePerson(PbfBlock.Create(data.ReadLengthPrefixedBytes()))); break;

                default: data.SkipField(wireType); break;
                }

                (fieldNumber, wireType) = data.ReadFieldHeader();
            }

            return(result);
        }