-
Notifications
You must be signed in to change notification settings - Fork 1
/
AddressBookDeserializer.cs
70 lines (56 loc) · 2.42 KB
/
AddressBookDeserializer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using PbfLite.Benchmark.Models;
namespace PbfLite.Benchmark.PbfLite {
static class AddressBookDeserializer {
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;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static PhoneNumber DeserializePhone(PbfBlock data) {
var result = new PhoneNumber();
var (fieldNumber, wireType) = data.ReadFieldHeader();
while (fieldNumber != 0) {
switch (fieldNumber) {
case 1: result.Number = data.ReadString(); break;
case 2: result.Type = (PhoneType)data.ReadInt(); break;
default: data.SkipField(wireType); break;
}
(fieldNumber, wireType) = data.ReadFieldHeader();
}
return result;
}
}
}