/// <summary>Helper: put the buffer into a MemoryStream and create a new instance to deserializing into</summary> internal static LocalFeatures Deserialize(byte[] buffer) { var instance = new LocalFeatures(); using (var ms = new MemoryStream(buffer)) Deserialize(ms, instance); return instance; }
public override bool Equals(object obj) { LocalFeatures l = obj as LocalFeatures; if (l == null) { return(false); } if (l.Uptime != this.Uptime) { return(false); } if (l.DueDate != this.DueDate) { return(false); } if (l.Amount != this.Amount) { return(false); } if (l.Denial != this.Denial) { return(false); } if (l.Secret != this.Secret) { return(false); } if (l.Internal != this.Internal) { return(false); } if (l.PR != this.PR) { return(false); } if (l.MyEnum != this.MyEnum) { return(false); } return(true); }
public void Run() { LocalFeatures local = new LocalFeatures("139pt2m7"); local.Uptime = TimeSpan.FromHours(37.8); local.DueDate = DateTime.UtcNow.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 Assert.IsTrue(local.Equals(l2), "Local Features"); //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 Assert.AreEqual(v2test.FieldA, v1.FieldA, "Modified in v1"); //Restore and test entire v2 v2test.FieldA = v2original.FieldA; Assert.IsTrue(v2original.Equals(v2test), "MyMessageV2 WriteRead"); }
/// <summary>Helper: Serialize with a varint length prefix</summary> internal static void SerializeLengthDelimited(Stream stream, LocalFeatures instance) { var data = SerializeToBytes(instance); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, (uint)data.Length); stream.Write(data, 0, data.Length); }
/// <summary>Helper: Serialize into a MemoryStream and return its byte array</summary> internal static byte[] SerializeToBytes(LocalFeatures instance) { using (var ms = new MemoryStream()) { Serialize(ms, instance); return ms.ToArray(); } }
/// <summary>Serialize the instance into the stream</summary> internal static void Serialize(Stream stream, LocalFeatures instance) { instance.BeforeSerialize(); var bw = new BinaryWriter(stream); var msField = global::SilentOrbit.ProtocolBuffers.ProtocolParser.Stack.Pop(); // Key for field: 1, Varint stream.WriteByte(8); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)instance.Uptime.Ticks); // Key for field: 2, Varint stream.WriteByte(16); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)(instance.DueDate.Kind == DateTimeKind.Utc ? instance.DueDate : instance.DueDate.ToUniversalTime()).Ticks); // Key for field: 3, Fixed64 stream.WriteByte(25); bw.Write(instance.Amount); if (instance.Denial != null) { // Key for field: 4, LengthDelimited stream.WriteByte(34); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteBytes(stream, Encoding.UTF8.GetBytes(instance.Denial)); } if (instance.Secret != null) { // Key for field: 5, LengthDelimited stream.WriteByte(42); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteBytes(stream, Encoding.UTF8.GetBytes(instance.Secret)); } if (instance.Internal != null) { // Key for field: 6, LengthDelimited stream.WriteByte(50); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteBytes(stream, Encoding.UTF8.GetBytes(instance.Internal)); } if (instance.PR != null) { // Key for field: 7, LengthDelimited stream.WriteByte(58); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteBytes(stream, Encoding.UTF8.GetBytes(instance.PR)); } if (instance.TestingReadOnly != null) { // Key for field: 8, LengthDelimited stream.WriteByte(66); msField.SetLength(0); Mine.MyMessageV1.Serialize(msField, instance.TestingReadOnly); // Length delimited byte array uint length8 = (uint)msField.Length; global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, length8); msField.WriteTo(stream); } if (instance.MyInterface == null) throw new global::SilentOrbit.ProtocolBuffers.ProtocolBufferException("MyInterface is required by the proto specification."); // Key for field: 9, LengthDelimited stream.WriteByte(74); msField.SetLength(0); LocalFeatureTest.InterfaceTestSerializer.Serialize(msField, instance.MyInterface); // Length delimited byte array uint length9 = (uint)msField.Length; global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, length9); msField.WriteTo(stream); // Key for field: 10, LengthDelimited stream.WriteByte(82); msField.SetLength(0); LocalFeatureTest.StructTest.Serialize(msField, instance.MyStruct); // Length delimited byte array uint length10 = (uint)msField.Length; global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, length10); msField.WriteTo(stream); // Key for field: 11, LengthDelimited stream.WriteByte(90); msField.SetLength(0); TestB.ExternalStructSerializer.Serialize(msField, instance.MyExtStruct); // Length delimited byte array uint length11 = (uint)msField.Length; global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, length11); msField.WriteTo(stream); if (instance.MyExtClass != null) { // Key for field: 12, LengthDelimited stream.WriteByte(98); msField.SetLength(0); TestB.ExternalClassSerializer.Serialize(msField, instance.MyExtClass); // Length delimited byte array uint length12 = (uint)msField.Length; global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, length12); msField.WriteTo(stream); } // Key for field: 13, Varint stream.WriteByte(104); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)instance.MyEnum); global::SilentOrbit.ProtocolBuffers.ProtocolParser.Stack.Push(msField); }
/// <summary>Helper: create a new instance to deserializing into</summary> internal static LocalFeatures DeserializeLength(Stream stream, int length) { var instance = new LocalFeatures(); DeserializeLength(stream, length, instance); return instance; }
/// <summary>Helper: create a new instance to deserializing into</summary> internal static LocalFeatures DeserializeLengthDelimited(Stream stream) { var instance = new LocalFeatures(); DeserializeLengthDelimited(stream, instance); return instance; }
/// <summary>Helper: create a new instance to deserializing into</summary> internal static LocalFeatures Deserialize(Stream stream) { var instance = new LocalFeatures(); Deserialize(stream, instance); return instance; }