protobuf-net
Protocol Buffers is the name of the binary serialization format used by Google for much of their data communications. It is designed to be: small in size - efficient data storage (far smaller than xml) cheap to process - both at the client and server platform independent - portable between different programming architectures extensible - to add new data to old messages. protobuf-net is a .NET implementation of this, allowing you to serialize your .NET objects efficiently and easily. It is compatible with most of the .NET family, including .NET 2.0/3.0/3.5/4.0, .NET CF 2.0/3.5, Mono 2.x, Silverlight, etc.
See the following samples where you can use it.
Step 1: Install protobuff
To install protobuf-net, run the following command in the Package Manager Console
PM> Install-Package protobuf-net
Step 2: Create the Data Models
Unlike XmlSerializer, the member-names are not encoded in the data - instead, you must pick an integer to identify each member. Additionally, to show intent it is necessary to show that we intend this type to be serialized. See the following sample.
- [ProtoContract]
- class Person {
- [ProtoMember(1)]
- public int Id {get;set;}
- [ProtoMember(2)]
- public string Name { get; set; }
- [ProtoMember(3)]
- public Address Address {get;set;}
- }
- [ProtoContract]
- class Address {
- [ProtoMember(1)]
- public string Line1 {get;set;}
- [ProtoMember(2)]
- public string Line2 {get;set;}
- }
Notes for Identifiers
- they must be positive integers
- they must be unique within a single type
- but the same numbers can be re-used in sub-types if inheritance is enabled
- the identifiers must not conflict with any inheritance identifiers lower numbers take less space - don't start 100,000,000
- the identifier is important; you can change the member-name, or shift it between a property and a field, but changing the identifier changes the data
Step 3: Serialize to stream
- public class ProtoBuf
- {
- public static MemoryStream Serialize(object content)
- {
- MemoryStream objectStream=new MemoryStream();
- Serializer.Serialize(objectStream,content);
- return objectStream;
- }
- public static T Deserialize<T>(MemoryStream objectStream)
- {
- objectStream.Position = 0;
- T content = Serializer.Deserialize<T>(objectStream);
- return content;
- }
- }
- var person = new Person
- {
- Id = 12345,
- Name = "Fred",
- Address = new Address
- {
- Line1 = "Flat 1",
- Line2 = "The Meadows"
- }
- };
- //serialize to stream
- MemoryStream stream= ProtoBuf.Serialize(person);
- //deserilize to person object
- stream.Position = 0;
- Person personObject = ProtoBuf.Deserialize<Person>(stream);
Step 3: Serialize and write to a file.
- var person = new Person
- {
- Id = 12345,
- Name = "Fred",
- Address = new Address
- {
- Line1 = "Flat 1",
- Line2 = "The Meadows"
- }
- };
- using (var file = File.Create("person.bin"))
- {
- Serializer.Serialize<Person>(file, person);
- }
- using (var file = File.OpenRead("person.bin"))
- {
- Person personObject = ProtoBuf.Deserialize<Person>(file);
- }
for more information in protobuf visit here
Happy Coding !!!
Comments
Post a Comment