As I recently discovered we can serialize/deserialize in protobuf-net using two classes ProtoBuf.Serializer and ProtoBuf.Meta.TypeModel.
For example, let's say we have a custom class that we want to serialize/deserialize:
[ProtoContract]
public class Person
{
[ProtoMember(1)]
public string Name { get; set; }
[ProtoMember(2)]
public int Age { get; set; }
}
We can do it in two ways:
1) Using TypeModel
TypeModel typeModel = RuntimeTypeModel.Default;
var person1 = new Person
{
Name = "John",
Age = 23
};
using (var stream = new MemoryStream())
{
typeModel.Serialize(stream, person1);
stream.Position = 0;
var pesrson2 = typeModel.Deserialize(stream, null, typeof(Person));
}
2) Using Serializer (which is the chosen way in most of the tutorials I've seen)
var person1 = new Person
{
Name = "John",
Age = 23
};
using (var stream = new MemoryStream())
{
Serializer.Serialize(stream, person1);
stream.Position = 0;
var pesrson2 = Serializer.Deserialize<Person>(stream);
}
What is the difference between these two approaches. How to choose between these two? And what is TypeModel and RuntimeTypeModel in the first place?