public void Run() { MyMessageV2 version2before = MyMessageV2.TestInstance(); //Write MemoryStream ms = new MemoryStream(); MyMessageV2.Serialize(ms, version2before); Console.WriteLine("Wire bytes: " + ms.Length); //Read MemoryStream msread = new MemoryStream(ms.ToArray()); MyMessageV2 version2after = MyMessageV2.Deserialize(msread); //Verify Assert.IsTrue(version2before.Equals(version2after), "MyMessageV2 WriteRead"); //Read by older version MemoryStream msread1 = new MemoryStream(ms.ToArray()); MyMessageV1 version1 = MyMessageV1.Deserialize(msread1); Console.WriteLine("Version 1"); Assert.AreEqual(version2before.FieldA, version1.FieldA); }
/// <summary>Helper: Serialize into a MemoryStream and return its byte array</summary> public static byte[] SerializeToBytes(MyMessageV1 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, MyMessageV1 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, MyMessageV1 instance) { // Key for field: 1, Varint stream.WriteByte(8); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)instance.FieldA); if (instance.PreservedFields != null) { foreach (var kv in instance.PreservedFields) { global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteKey(stream, kv.Key); stream.Write(kv.Value, 0, kv.Value.Length); } } }
/// <summary>Helper: create a new instance to deserializing into</summary> public static MyMessageV1 DeserializeLengthDelimited(Stream stream) { MyMessageV1 instance = new MyMessageV1(); DeserializeLengthDelimited(stream, instance); return instance; }
/// <summary>Helper: create a new instance to deserializing into</summary> public static MyMessageV1 DeserializeLength(Stream stream, int length) { MyMessageV1 instance = new MyMessageV1(); 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 MyMessageV1 Deserialize(byte[] buffer) { MyMessageV1 instance = new MyMessageV1(); using (var ms = new MemoryStream(buffer)) Deserialize(ms, instance); return instance; }
/// <summary>Helper: create a new instance to deserializing into</summary> public static MyMessageV1 Deserialize(Stream stream) { MyMessageV1 instance = new MyMessageV1(); Deserialize(stream, instance); return instance; }
/// <summary> /// Test wire format of the person example against protobuf-net - another c# protocol buffers library /// </summary> public static void Run() { LocalFeatures local = new LocalFeatures("139pt2m7"); local.Uptime = TimeSpan.FromHours(37.8); local.DueDate = DateTime.Now.AddMinutes(1); local.Internal = "assembly"; local.PR = "Hi"; local.Amount = Math.E; local.Deny("they exist"); local.MyInterface = new MyImplementeInterface(); local.MyEnum = LocalFeatureTest.TopEnum.Last; MemoryStream ms1 = new MemoryStream(); LocalFeatures.Serialize(ms1, local); MemoryStream ms2 = new MemoryStream(ms1.ToArray()); LocalFeatures l2 = new LocalFeatures("Secret"); //Since this property is an interface AND required we must set it before calling Deserialize l2.MyInterface = new MyImplementeInterface(); LocalFeatures.Deserialize(ms2, l2); //Test in Equals to have access to all fields Test("Local Features", local.Equals(l2)); //Test preservation of unknown fields byte[] streamBuffer; byte[] streamBufferV2Orig; byte[] streamBufferV1Mod; MyMessageV2 v2original = MyMessageV2.TestInstance(); //Write using (MemoryStream ms = new MemoryStream()) { MyMessageV2.Serialize(ms, v2original); streamBuffer = ms.ToArray(); streamBufferV2Orig = streamBuffer; } //Read V1, modify and write back MyMessageV1 v1 = MyMessageV1.Deserialize(new MemoryStream(streamBuffer)); v1.FieldA = 42; using (MemoryStream ms = new MemoryStream()) { MyMessageV1.Serialize(ms, v1); streamBuffer = ms.ToArray(); streamBufferV1Mod = streamBuffer; } //Compare stream buffers //Test ( // "Stream buffer length", // streamBufferV2Orig.Length == streamBufferV1Mod.Length //); for (int n = 0; n < streamBufferV2Orig.Length; n++) { if (n == 1) { continue; //expected difference for FieldA } if (streamBufferV2Orig [n] != streamBufferV1Mod [n]) { throw new InvalidDataException("Stream buffers do not match at byte " + n); } } //Read V2 and test MyMessageV2 v2test = MyMessageV2.Deserialize(new MemoryStream(streamBuffer)); //Test FieldA Test("Modified in v1", v2test.FieldA == v1.FieldA); //Restore and test entire v2 v2test.FieldA = v2original.FieldA; Test("MyMessageV2 WriteRead", v2original.Equals(v2test)); }