// 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); }
// Start is called before the first frame update void Start() { Tutorial.Person person = new Tutorial.Person(); person.Email = "*****@*****.**"; person.Id = 666; person.Name = "linsh"; Tutorial.PhoneNumber phoneNum = new Tutorial.PhoneNumber(); phoneNum.Number = "555-4321"; phoneNum.Type = Tutorial.PhoneType.Home; person.Phones.Add(phoneNum); // 序列化 byte[] buff; MemoryStream stream = new MemoryStream(); // Save the person to a stream person.WriteTo(stream); buff = stream.ToArray(); // 反序列化 Tutorial.Person person1 = Tutorial.Person.Parser.ParseFrom(buff); Debug.LogFormat("person info = Email = {0}, Name = {1}, Id = {2} , Phones[0].Number = {3}", person1.Email, person1.Name, person1.Id, person1.Phones[0].Number); }
/// <summary>Helper: Serialize into a MemoryStream and return its byte array</summary> public static byte[] SerializeToBytes(Person 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, Person 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, Person instance) { var msField = global::SilentOrbit.ProtocolBuffers.ProtocolParser.Stack.Pop(); if (instance.Name == null) throw new global::SilentOrbit.ProtocolBuffers.ProtocolBufferException("Name is required by the proto specification."); // Key for field: 1, LengthDelimited stream.WriteByte(10); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteBytes(stream, Encoding.UTF8.GetBytes(instance.Name)); // Key for field: 2, Varint stream.WriteByte(16); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)instance.Id); if (instance.Email != null) { // Key for field: 3, LengthDelimited stream.WriteByte(26); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteBytes(stream, Encoding.UTF8.GetBytes(instance.Email)); } if (instance.Phone != null) { foreach (var i4 in instance.Phone) { // Key for field: 4, LengthDelimited stream.WriteByte(34); msField.SetLength(0); Tutorial.Person.PhoneNumber.Serialize(msField, i4); // Length delimited byte array uint length4 = (uint)msField.Length; global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, length4); msField.WriteTo(stream); } } global::SilentOrbit.ProtocolBuffers.ProtocolParser.Stack.Push(msField); }
/// <summary>Helper: create a new instance to deserializing into</summary> public static Person DeserializeLengthDelimited(Stream stream) { var instance = new Person(); DeserializeLengthDelimited(stream, instance); return instance; }
/// <summary>Helper: create a new instance to deserializing into</summary> public static Person DeserializeLength(Stream stream, int length) { var instance = new Person(); DeserializeLength(stream, length, instance); return instance; }
/// <summary>Helper: put the buffer into a MemoryStream and create a new instance to deserializing into</summary> public static Person Deserialize(byte[] buffer) { var instance = new Person(); using (var ms = new MemoryStream(buffer)) Deserialize(ms, instance); return instance; }