/// <summary>Helper: put the buffer into a MemoryStream and create a new instance to deserializing into</summary> public static AddressBook Deserialize(byte[] buffer) { var instance = new AddressBook(); using (var ms = new MemoryStream(buffer)) Deserialize(ms, instance); return instance; }
// Use this for initialization void Start() { Tutorial.AddressBook addressbook = new Tutorial.AddressBook(); Tutorial.Person person = new Tutorial.Person(); person.Id = 5; person.Name = "sun"; person.Phone = new List<Tutorial.Person.PhoneNumber>(); var phoneNumItem = new Tutorial.Person.PhoneNumber(); phoneNumItem.Number = "1862015"; phoneNumItem.Type = Tutorial.Person.PhoneType.MOBILE; person.Phone.Add(phoneNumItem); person.Email = "*****@*****.**"; addressbook.Person = new List<Tutorial.Person>(); addressbook.Person.Add(person); uint serializeSize = addressbook.GetSerializedSize(); var buffer = Tutorial.AddressBook.SerializeToBytes(addressbook); var address = Tutorial.AddressBook.Deserialize(buffer); var buffer2 = ProtobufUtil.ToByteArray(addressbook); var address2 = ProtobufUtil.ParseFrom<Tutorial.AddressBook>(buffer2); }
/// <summary>Helper: create a new instance to deserializing into</summary> public static AddressBook DeserializeLength(Stream stream, int length) { var instance = new AddressBook(); DeserializeLength(stream, length, instance); return instance; }
/// <summary>Helper: Serialize into a MemoryStream and return its byte array</summary> public static byte[] SerializeToBytes(AddressBook instance) { using (var ms = new MemoryStream()) { Serialize(ms, instance); return ms.ToArray(); } }
/// <summary>Helper: Serialize with a varint length prefix</summary> public static void SerializeLengthDelimited(Stream stream, AddressBook instance) { var data = SerializeToBytes(instance); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, (uint)data.Length); stream.Write(data, 0, data.Length); }
/// <summary>Serialize the instance into the stream</summary> public static void Serialize(Stream stream, AddressBook instance) { var msField = global::SilentOrbit.ProtocolBuffers.ProtocolParser.Stack.Pop(); if (instance.Person != null) { foreach (var i1 in instance.Person) { // Key for field: 1, LengthDelimited stream.WriteByte(10); msField.SetLength(0); Tutorial.Person.Serialize(msField, i1); // Length delimited byte array uint length1 = (uint)msField.Length; global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, length1); msField.WriteTo(stream); } } global::SilentOrbit.ProtocolBuffers.ProtocolParser.Stack.Push(msField); }
/// <summary>Helper: create a new instance to deserializing into</summary> public static AddressBook DeserializeLengthDelimited(Stream stream) { var instance = new AddressBook(); DeserializeLengthDelimited(stream, instance); return instance; }