public void CheckForCycles_FRN49() { var context = new FudgeContext(); var serializer = new FudgeSerializer(context); var testObj = new ClassWithCycles(); testObj.Child = new ClassWithCycles(); serializer.SerializeToMsg(testObj); // Doesn't throw because no cycles testObj.Child = testObj; Assert.Throws<FudgeRuntimeException>(() => serializer.SerializeToMsg(testObj)); }
public void CheckForCycles_FRN49() { var context = new FudgeContext(); var serializer = new FudgeSerializer(context); var testObj = new ClassWithCycles(); testObj.Child = new ClassWithCycles(); serializer.SerializeToMsg(testObj); // Doesn't throw because no cycles testObj.Child = testObj; Assert.Throws <FudgeRuntimeException>(() => serializer.SerializeToMsg(testObj)); }
public void CircularReference() { var typeMap = new SerializationTypeMap(context); typeMap.RegisterType(typeof(Explicit.Sibling)); typeMap.RegisterType(typeof(Explicit.Address), new Explicit.AddressSerializer()); var serializer = new FudgeSerializer(context, typeMap); var bob = new Explicit.Sibling { Name = "Bob" }; var shirley = new Explicit.Sibling { Name = "Shirley" }; bob.Siblings.Add(shirley); shirley.Siblings.Add(bob); // Create our cycle var msg = serializer.SerializeToMsg(bob); var bob2 = (Explicit.Sibling)serializer.Deserialize(msg); Assert.NotSame(bob, bob2); Assert.Equal(1, bob2.Siblings.Count); var shirley2 = (Explicit.Sibling)bob2.Siblings[0]; Assert.NotSame(shirley, shirley2); Assert.Equal(1, shirley2.Siblings.Count); Assert.Same(bob2, shirley2.Siblings[0]); }
public void SerializingNullsInLists_FRN52() { var context = new FudgeContext(); var testClass = new TestClass { List = new List <SomeClass>() }; testClass.List.Add(new SomeClass { Name = "A" }); testClass.List.Add(null); testClass.List.Add(new SomeClass { Name = "B" }); testClass.Array = new SomeInlineClass[] { new SomeInlineClass { Name = "C" }, null, new SomeInlineClass { Name = "D" } }; var serializer = new FudgeSerializer(context); var msg = serializer.SerializeToMsg(testClass); var testClass2 = (TestClass)serializer.Deserialize(msg); Assert.Equal("A", testClass2.List[0].Name); Assert.Null(testClass2.List[1]); Assert.Equal("B", testClass2.List[2].Name); Assert.Equal("C", testClass2.Array[0].Name); Assert.Null(testClass2.Array[1]); Assert.Equal("D", testClass2.Array[2].Name); }
public void ReferencedObject() { var typeMap = new SerializationTypeMap(context); typeMap.RegisterType(typeof(Explicit.Sibling)); typeMap.RegisterType(typeof(Explicit.Address), new Explicit.AddressSerializer()); var serializer = new FudgeSerializer(context, typeMap); var bob = new Explicit.Sibling { Name = "Bob" }; var shirley = new Explicit.Sibling { Name = "Shirley" }; bob.Siblings.Add(shirley); // We don't reciprocate yet as that would generate a cycle var msg = serializer.SerializeToMsg(bob); var bob2 = (Explicit.Sibling)serializer.Deserialize(msg); Assert.NotSame(bob, bob2); Assert.Equal(1, bob2.Siblings.Count); Assert.NotSame(shirley, bob2.Siblings[0]); Assert.Equal("Shirley", bob2.Siblings[0].Name); }
public void CycleInImmutable_FRN71() { var outer = new ImmutableCycle1(new ImmutableCycle2()); outer.Other.Other = outer; var serializer = new FudgeSerializer(context); var msg = serializer.SerializeToMsg(outer); var outer2 = (ImmutableCycle1)serializer.Deserialize(msg); Assert.Same(outer2, outer2.Other.Other); }
public void HonoursDataMemberName() { var obj1 = new SimpleTestClass { SerializedMember = "Serialized", UnserializedMember = "Unserialized", SerializedProperty = 1, UnserializedProperty = 2 }; var serializer = new FudgeSerializer(context); var msg = serializer.SerializeToMsg(obj1); Assert.NotNull(msg.GetByName("Prop")); }
public void ToFudgeMsg(IAppendingFudgeFieldContainer calcConfigMsg, IFudgeSerializer s) { var context = s.Context; var fudgeSerializer = new FudgeSerializer(context); calcConfigMsg.Add("name", Name); foreach (var securityTypeRequirements in PortfolioRequirementsBySecurityType) { FudgeMsg securityTypeRequirementsMsg = new FudgeMsg(context); securityTypeRequirementsMsg.Add("securityType", securityTypeRequirements.Key); foreach (var requirement in securityTypeRequirements.Value) { var newMessage = context.NewMessage(); newMessage.Add("requiredOutput", requirement.Item1); newMessage.Add("constraints", fudgeSerializer.SerializeToMsg(requirement.Item2)); securityTypeRequirementsMsg.Add("portfolioRequirement", newMessage); } calcConfigMsg.Add("portfolioRequirementsBySecurityType", securityTypeRequirementsMsg); } foreach (var specificRequirement in SpecificRequirements) { var sReqMsg = fudgeSerializer.SerializeToMsg(specificRequirement); calcConfigMsg.Add("specificRequirement", sReqMsg); } var defaultPropsMessage = fudgeSerializer.SerializeToMsg(DefaultProperties); calcConfigMsg.Add("defaultProperties", defaultPropsMessage); var deltaDefnMessage = fudgeSerializer.SerializeToMsg(_deltaDefinition); calcConfigMsg.Add("deltaDefinition", deltaDefnMessage); if (ResolutionRuleTransform != null) { var transformMessage = fudgeSerializer.SerializeToMsg(ResolutionRuleTransform); calcConfigMsg.Add("resolutionRuleTransform", transformMessage); } }
public void SimpleCase() { var serializer = new FudgeSerializer(context); var obj1 = new SimpleClass("Test", "Case"); var msg = serializer.SerializeToMsg(obj1); var obj2 = (SimpleClass)serializer.Deserialize(msg); Assert.Equal(obj1.Val1, obj2.Val1); }
public void EnumSurrogateCalled() { var a = SurrogateEnumTest.A; var serializer = new FudgeSerializer(context); var msg = serializer.SerializeToMsg(a); var obj2 = (SurrogateEnumTest)serializer.Deserialize(msg); Assert.Equal(a, obj2); }
public void RoundTrip() { var serializer = new FudgeSerializer(context); var obj1 = new ExampleClass(7); var msg = serializer.SerializeToMsg(obj1); var obj2 = (ExampleClass)serializer.Deserialize(msg); Assert.NotSame(obj1, obj2); Assert.Equal(obj1.Number, obj2.Number); }
public void HonoursDataMemberName() { var obj1 = new SimpleTestClass { SerializedMember = "Serialized", UnserializedMember = "Unserialized", SerializedProperty = 1, UnserializedProperty = 2 }; var serializer = new FudgeSerializer(context); var msg = serializer.SerializeToMsg(obj1); Assert.NotNull(msg.GetByName("Prop")); }
public void CycleInImmutable_FRN71() { var outer = new ImmutableCycle1(new ImmutableCycle2()); outer.Other.Other = outer; var serializer = new FudgeSerializer(context); var msg = serializer.SerializeToMsg(outer); var outer2 = (ImmutableCycle1)serializer.Deserialize(msg); Assert.Same(outer2, outer2.Other.Other); }
public void EnumSurrogateCalled() { var a = SurrogateEnumTest.A; var serializer = new FudgeSerializer(context); var msg = serializer.SerializeToMsg(a); var obj2 = (SurrogateEnumTest)serializer.Deserialize(msg); Assert.Equal(a, obj2); }
public void RoundTrip() { var serializer = new FudgeSerializer(context); var obj1 = new ExampleClass(7); var msg = serializer.SerializeToMsg(obj1); var obj2 = (ExampleClass)serializer.Deserialize(msg); Assert.NotSame(obj1, obj2); Assert.Equal(obj1.Number, obj2.Number); }
public void HandlesInnerObjects() { var obj1 = new ClassWithInner { Inner = new ClassWithInner() }; var serializer = new FudgeSerializer(context); var msg = serializer.SerializeToMsg(obj1); var obj2 = (ClassWithInner)serializer.Deserialize(msg); Assert.NotNull(obj2.Inner); Assert.NotSame(obj2, obj2.Inner); }
public void NonSerializedMarkerTest() { var obj1 = new SerializableClass(2, 3); var serializer = new FudgeSerializer(context); var msg = serializer.SerializeToMsg(obj1); var obj2 = (SerializableClass)serializer.Deserialize(msg); Assert.Equal(2, obj2.GetSerializableField()); Assert.Equal(0, obj2.GetNonserializableField()); }
public void NullHandling() { var obj1 = new ClassWithInner(); // Not setting the inner var serializer = new FudgeSerializer(context); var msg = serializer.SerializeToMsg(obj1); var obj2 = (ClassWithInner)serializer.Deserialize(msg); Assert.NotNull(obj2); Assert.Null(obj2.Inner); }
public void HonoursBeforeAndAfterSerialize_FRN78() { var obj1 = new ClassWithBeforeAndAfterMethods { Val1 = "Test" }; var serializer = new FudgeSerializer(context); var msg = serializer.SerializeToMsg(obj1); Assert.Equal("Before|Test", msg.GetString("Val1")); Assert.Equal("Before|Test|After", obj1.Val1); var obj2 = (ClassWithBeforeAndAfterMethods)serializer.Deserialize(msg); Assert.Equal("Before|Test", obj2.Val1); Assert.Equal("null|After2", obj2.Val2); }
public void BaseTypesOutputAsWell_FRN43() { var serializer = new FudgeSerializer(context); var bob = new Explicit.Sibling { Name = "Bob" }; var msg = serializer.SerializeToMsg(bob); var typeNames = msg.GetAllValues<string>(FudgeSerializer.TypeIdFieldOrdinal); Assert.Equal(2, typeNames.Count); Assert.Equal("Fudge.Tests.Unit.Serialization.Explicit+Sibling", typeNames[0]); Assert.Equal("Fudge.Tests.Unit.Serialization.Explicit+Person", typeNames[1]); }
public static FudgeMsg ToFudgeMsg <TKey, TValue>(IFudgeSerializer s, IDictionary <TKey, TValue> dict, Func <TKey, object> keyMsgGen = null, Func <TValue, object> valueMsgGen = null) where TValue : class { var fudgeSerializer = new FudgeSerializer(s.Context); keyMsgGen = keyMsgGen ?? (k => fudgeSerializer.SerializeToMsg(k)); valueMsgGen = valueMsgGen ?? (v => fudgeSerializer.SerializeToMsg(v)); var valuesMessage = new FudgeMsg(s.Context); foreach (var value in dict) { valuesMessage.Add(1, keyMsgGen(value.Key)); if (value.Value == null) { valuesMessage.Add(2, IndicatorType.Instance); } else { valuesMessage.Add(2, valueMsgGen(value.Value)); } } return(valuesMessage); }
public void HandlesInnerObjects() { var obj1 = new ClassWithInner { Inner = new ClassWithInner() }; var serializer = new FudgeSerializer(context); var msg = serializer.SerializeToMsg(obj1); var obj2 = (ClassWithInner)serializer.Deserialize(msg); Assert.NotNull(obj2.Inner); Assert.NotSame(obj2, obj2.Inner); }
public void NamingConventionsApplied() { var obj1 = new SimpleTestClass { SerializedMember = "Serialized", UnserializedMember = "Unserialized", SerializedProperty = 1, UnserializedProperty = 2 }; var serializer = new FudgeSerializer(context); serializer.TypeMap.FieldNameConvention = FudgeFieldNameConvention.AllUpperCase; var msg = serializer.SerializeToMsg(obj1); Assert.NotNull(msg.GetByName("SERIALIZEDMEMBER")); }
public void PropertiesArentSerialized() { var obj1 = new PropertyTest(); PropertyTest.val = 7; var serializer = new FudgeSerializer(context); var msg = serializer.SerializeToMsg(obj1); PropertyTest.val = 0; var obj2 = (PropertyTest)serializer.Deserialize(msg); Assert.Equal(0, PropertyTest.val); }
public void SimpleCase() { var obj1 = new SimpleTestClass { Val = "Test" }; var serializer = new FudgeSerializer(context); var msg = serializer.SerializeToMsg(obj1); Assert.Equal("Test", msg.GetString("serializedVal")); var obj2 = (SimpleTestClass)serializer.Deserialize(msg); Assert.Equal("Test", obj2.Val); }
public void FieldNameConventionsWithAttribute() { var obj1 = new FieldConventionAttributeClass { MyName = "Fred" }; // Specifies camelCase var serializer = new FudgeSerializer(context); var msg = serializer.SerializeToMsg(obj1); Assert.NotNull(msg.GetByName("MYNAME")); var obj2 = (FieldConventionAttributeClass)serializer.Deserialize(msg); Assert.Equal(obj1.MyName, obj2.MyName); }
public void ArrayOfSubObjects() { Assert.True(PropertyBasedSerializationSurrogate.CanHandle(typeDataCache, FudgeFieldNameConvention.Identity, typeof(ArrayOfObjectsClass))); var serializer = new FudgeSerializer(context); // We're relying on it auto-discovering the type surrogate var obj1 = new ArrayOfObjectsClass(); obj1.Subs = new SimpleExampleClass[] {new SimpleExampleClass { Name = "Bob", Age = 21 }}; var msg = serializer.SerializeToMsg(obj1); var obj2 = (ArrayOfObjectsClass)serializer.Deserialize(msg); Assert.NotSame(obj1, obj2); Assert.NotSame(obj1.Subs[0], obj2.Subs[0]); Assert.Equal(obj1.Subs[0].Name, obj2.Subs[0].Name); }
public void BaseTypesOutputAsWell_FRN43() { var serializer = new FudgeSerializer(context); var bob = new Explicit.Sibling { Name = "Bob" }; var msg = serializer.SerializeToMsg(bob); var typeNames = msg.GetAllValues <string>(FudgeSerializer.TypeIdFieldOrdinal); Assert.Equal(2, typeNames.Count); Assert.Equal("Fudge.Tests.Unit.Serialization.Explicit+Sibling", typeNames[0]); Assert.Equal("Fudge.Tests.Unit.Serialization.Explicit+Person", typeNames[1]); }
public void SecondaryTypes() { Assert.True(PropertyBasedSerializationSurrogate.CanHandle(typeDataCache, FudgeFieldNameConvention.Identity, typeof(SecondaryTypeClass))); var serializer = new FudgeSerializer(context); // We're relying on it auto-discovering the type surrogate var obj1 = new SecondaryTypeClass { Id = Guid.NewGuid() }; var msg = serializer.SerializeToMsg(obj1); var obj2 = (SecondaryTypeClass)serializer.Deserialize(msg); Assert.NotSame(obj1, obj2); Assert.Equal(obj1.Id, obj2.Id); }
public void SimpleExampleWithSurrogate() { var typeMap = new SerializationTypeMap(context); typeMap.RegisterType(typeof(Explicit.Address), new Explicit.AddressSerializer()); var serializer = new FudgeSerializer(context, typeMap); var address = new Explicit.Address("Our House", "In the middle of our street", "MD1"); var msg = serializer.SerializeToMsg(address); var address2 = (Explicit.Address)serializer.Deserialize(msg); Assert.Equal(address.Line1, address2.Line1); Assert.Equal(address.Line2, address2.Line2); Assert.Equal(address.Zip, address2.Zip); }
public void SimpleExample() { Assert.True(PropertyBasedSerializationSurrogate.CanHandle(typeDataCache, FudgeFieldNameConvention.Identity, typeof(SimpleExampleClass))); var serializer = new FudgeSerializer(context); // We're relying on it auto-discovering the type surrogate var obj1 = new SimpleExampleClass { Name = "Dennis", Age = 37 }; var msg = serializer.SerializeToMsg(obj1); var obj2 = (SimpleExampleClass)serializer.Deserialize(msg); Assert.NotSame(obj1, obj2); Assert.Equal(obj1.Name, obj2.Name); Assert.Equal(obj1.Age, obj2.Age); }
public void HonoursBeforeAndAfterSerialize_FRN78() { var obj1 = new ClassWithBeforeAndAfterMethods { Val1 = "Test" }; var serializer = new FudgeSerializer(context); var msg = serializer.SerializeToMsg(obj1); Assert.Equal("Before|Test", msg.GetString("Val1")); Assert.Equal("Before|Test|After", obj1.Val1); var obj2 = (ClassWithBeforeAndAfterMethods)serializer.Deserialize(msg); Assert.Equal("Before|Test", obj2.Val1); Assert.Equal("null|After2", obj2.Val2); }
public void ObjectIdentityNotEquals_FRN65() { // Using GetHashCode and Equals is not good enough for testing object identity // FRN65Class always returns true for Equals and a constant for GetHashCode var obj1 = new FRN65Class { Val = "A", Other = new FRN65Class { Val = "B" } }; var serializer = new FudgeSerializer(context); var msg = serializer.SerializeToMsg(obj1); var obj2 = (FRN65Class)serializer.Deserialize(msg); Assert.NotSame(obj2, obj2.Other); }
public void InlineAttribute_FRN48() { var serializer = new FudgeSerializer(context); var parent = new InlineParent(); parent.SetUp(); var msg = serializer.SerializeToMsg(parent); Assert.Equal(FudgeMsgFieldType.Instance, msg.GetByName("In1").Type); Assert.Equal(FudgeMsgFieldType.Instance, msg.GetByName("In2").Type); Assert.Equal(PrimitiveFieldTypes.SByteType, msg.GetByName("In1ForcedOut").Type); // References In1 and collapses to byte Assert.Equal(FudgeMsgFieldType.Instance, msg.GetByName("Out1").Type); Assert.Equal(PrimitiveFieldTypes.SByteType, msg.GetByName("Out2").Type); // References Out1 Assert.Equal(FudgeMsgFieldType.Instance, msg.GetByName("Out2ForcedIn").Type); }
public void FieldNameConventionsWithContextProperty() { var context = new FudgeContext(); // So we don't mess with other unit tests var obj1 = new FieldConventionClass { MyName = "Bobby", myAge = 6 }; FudgeMsg msg; FudgeSerializer serializer; // Default is identity serializer = new FudgeSerializer(context); Assert.Equal(FudgeFieldNameConvention.Identity, serializer.TypeMap.FieldNameConvention); msg = serializer.SerializeToMsg(obj1); Assert.Equal("Bobby", msg.GetString("MyName")); Assert.Equal(6, msg.GetInt("myAge")); Assert.Equal(obj1, serializer.Deserialize(msg)); context.SetProperty(ContextProperties.FieldNameConventionProperty, FudgeFieldNameConvention.AllLowerCase); serializer = new FudgeSerializer(context); msg = serializer.SerializeToMsg(obj1); Assert.Equal("Bobby", msg.GetString("myname")); Assert.Equal(6, msg.GetInt("myage")); Assert.Equal(obj1, serializer.Deserialize(msg)); context.SetProperty(ContextProperties.FieldNameConventionProperty, FudgeFieldNameConvention.AllUpperCase); serializer = new FudgeSerializer(context); msg = serializer.SerializeToMsg(obj1); Assert.Equal("Bobby", msg.GetString("MYNAME")); Assert.Equal(6, msg.GetInt("MYAGE")); Assert.Equal(obj1, serializer.Deserialize(msg)); context.SetProperty(ContextProperties.FieldNameConventionProperty, FudgeFieldNameConvention.CamelCase); serializer = new FudgeSerializer(context); msg = serializer.SerializeToMsg(obj1); Assert.Equal("Bobby", msg.GetString("myName")); Assert.Equal(6, msg.GetInt("myAge")); Assert.Equal(obj1, serializer.Deserialize(msg)); context.SetProperty(ContextProperties.FieldNameConventionProperty, FudgeFieldNameConvention.PascalCase); serializer = new FudgeSerializer(context); msg = serializer.SerializeToMsg(obj1); Assert.Equal("Bobby", msg.GetString("MyName")); Assert.Equal(6, msg.GetInt("MyAge")); Assert.Equal(obj1, serializer.Deserialize(msg)); }
public void ListOfArrays() { Assert.True(PropertyBasedSerializationSurrogate.CanHandle(typeDataCache, FudgeFieldNameConvention.Identity, typeof(ListOfArraysClass))); var serializer = new FudgeSerializer(context); // We're relying on it auto-discovering the type surrogate var obj1 = new ListOfArraysClass(); obj1.List = new List <string[]>(); obj1.List.Add(new string[] { "Bob", "Mavis" }); var msg = serializer.SerializeToMsg(obj1); var obj2 = (ListOfArraysClass)serializer.Deserialize(msg); Assert.NotSame(obj1, obj2); Assert.NotSame(obj1.List[0], obj2.List[0]); Assert.Equal(obj1.List[0], obj2.List[0]); }
public void SimpleExampleWithIFudgeSerializable() { var typeMap = new SerializationTypeMap(context); typeMap.RegisterType(typeof(Explicit.Tick)); var serializer = new FudgeSerializer(context, typeMap); var tick = new Explicit.Tick { Ticker = "FOO", Bid = 12.3, Offer = 12.9 }; var msg = serializer.SerializeToMsg(tick); var tick2 = (Explicit.Tick)serializer.Deserialize(msg); Assert.Equal(tick.Ticker, tick2.Ticker); Assert.Equal(tick.Bid, tick2.Bid); Assert.Equal(tick.Offer, tick2.Offer); }
public void SimpleCase() { var obj1 = new SimpleTestClass { SerializedMember = "Serialized", UnserializedMember = "Unserialized", SerializedProperty = 1, UnserializedProperty = 2 }; var serializer = new FudgeSerializer(context); var msg = serializer.SerializeToMsg(obj1); var obj2 = (SimpleTestClass)serializer.Deserialize(msg); Assert.Equal(obj1.SerializedMember, obj2.SerializedMember); Assert.Equal(obj1.SerializedProperty, obj2.SerializedProperty); // Make sure the others weren't serialized Assert.Null(obj2.UnserializedMember); Assert.Equal(0, obj2.UnserializedProperty); }
public void InlineObject() { var typeMap = new SerializationTypeMap(context); typeMap.RegisterType(typeof(Explicit.Person)); typeMap.RegisterType(typeof(Explicit.Address), new Explicit.AddressSerializer()); var serializer = new FudgeSerializer(context, typeMap); var person = new Explicit.Person { Name = "Bob", MainAddress = new Explicit.Address("Foo", "Bar", null) }; var msg = serializer.SerializeToMsg(person); var person2 = (Explicit.Person)serializer.Deserialize(msg); Assert.NotSame(person.MainAddress, person2.MainAddress); Assert.Equal(person.MainAddress.Line1, person2.MainAddress.Line1); }
public void UsingISerializationSurrogates() { var surrogateSelector = new SurrogateSelector(); var streamingContext = new StreamingContext(StreamingContextStates.All); surrogateSelector.AddSurrogate(typeof(ClassWithSurrogate), streamingContext, new SurrogateClass()); var serializer = new FudgeSerializer(context); serializer.TypeMap.RegisterSurrogateSelector(surrogateSelector); // Check out the surrogate var surrogate = serializer.TypeMap.GetSurrogate(typeof(ClassWithSurrogate)); Assert.IsType<DotNetSerializationSurrogateSurrogate>(surrogate); Assert.IsType<SurrogateClass>(((DotNetSerializationSurrogateSurrogate)surrogate).SerializationSurrogate); var obj1 = new ClassWithSurrogate { A = 22 }; var msg = serializer.SerializeToMsg(obj1); var obj2 = (ClassWithSurrogate)serializer.Deserialize(msg); Assert.Equal(obj1.A, obj2.A); }
public void SerializingNullsInLists_FRN52() { var context = new FudgeContext(); var testClass = new TestClass { List = new List<SomeClass>() }; testClass.List.Add(new SomeClass { Name = "A" }); testClass.List.Add(null); testClass.List.Add(new SomeClass { Name = "B" }); testClass.Array = new SomeInlineClass[] { new SomeInlineClass { Name = "C" }, null, new SomeInlineClass { Name = "D" } }; var serializer = new FudgeSerializer(context); var msg = serializer.SerializeToMsg(testClass); var testClass2 = (TestClass)serializer.Deserialize(msg); Assert.Equal("A", testClass2.List[0].Name); Assert.Null(testClass2.List[1]); Assert.Equal("B", testClass2.List[2].Name); Assert.Equal("C", testClass2.Array[0].Name); Assert.Null(testClass2.Array[1]); Assert.Equal("D", testClass2.Array[2].Name); }
public void CircularReference() { var typeMap = new SerializationTypeMap(context); typeMap.RegisterType(typeof(Explicit.Sibling)); typeMap.RegisterType(typeof(Explicit.Address), new Explicit.AddressSerializer()); var serializer = new FudgeSerializer(context, typeMap); var bob = new Explicit.Sibling { Name = "Bob" }; var shirley = new Explicit.Sibling { Name = "Shirley" }; bob.Siblings.Add(shirley); shirley.Siblings.Add(bob); // Create our cycle var msg = serializer.SerializeToMsg(bob); var bob2 = (Explicit.Sibling)serializer.Deserialize(msg); Assert.NotSame(bob, bob2); Assert.Equal(1, bob2.Siblings.Count); var shirley2 = (Explicit.Sibling)bob2.Siblings[0]; Assert.NotSame(shirley, shirley2); Assert.Equal(1, shirley2.Siblings.Count); Assert.Same(bob2, shirley2.Siblings[0]); }
private void Cycle(string msg, object obj, int nCycles) { Console.Out.Write((msg + ":").PadRight(padWidth)); var serializer = new FudgeSerializer(context); serializer.SerializeToMsg(obj); // Just get the reflection stuff out of the way var stopWatch = new Stopwatch(); var stream = new MemoryStream(); var writer = new FudgeEncodedStreamWriter(context, stream); var reader = new FudgeEncodedStreamReader(context, stream); stopWatch.Start(); for (int i = 0; i < nCycles; i++) { stream.Position = 0; serializer.Serialize(writer, obj); stream.Flush(); stream.Position = 0; var obj2 = serializer.Deserialize(reader, null); } stopWatch.Stop(); double speed = (double)Stopwatch.Frequency * nCycles / stopWatch.ElapsedTicks; Console.Out.WriteLine(String.Format("{0:F0}/s", speed)); }
public void SimpleExampleWithIFudgeSerializable() { var typeMap = new SerializationTypeMap(context); typeMap.RegisterType(typeof(Explicit.Tick)); var serializer = new FudgeSerializer(context, typeMap); var tick = new Explicit.Tick { Ticker = "FOO", Bid = 12.3, Offer = 12.9 }; var msg = serializer.SerializeToMsg(tick); var tick2 = (Explicit.Tick)serializer.Deserialize(msg); Assert.Equal(tick.Ticker, tick2.Ticker); Assert.Equal(tick.Bid, tick2.Bid); Assert.Equal(tick.Offer, tick2.Offer); }
public void SerializingNulls_FRN51() { var serializer = new FudgeSerializer(context); var parent = new InlineParent(); parent.In1 = null; parent.In2 = new Inlined(); parent.In1ForcedOut = null; parent.Out1 = null; parent.Out2 = new NotInlined(); parent.Out2ForcedIn = parent.Out2; var msg = serializer.SerializeToMsg(parent); var parent2 = (InlineParent)serializer.Deserialize(msg); Assert.Null(parent2.In1); Assert.NotNull(parent2.In2); Assert.Null(parent2.In1ForcedOut); Assert.Null(parent2.Out1); Assert.NotNull(parent2.Out2); Assert.NotNull(parent2.Out2ForcedIn); }
public void ReferencedObject() { var typeMap = new SerializationTypeMap(context); typeMap.RegisterType(typeof(Explicit.Sibling)); typeMap.RegisterType(typeof(Explicit.Address), new Explicit.AddressSerializer()); var serializer = new FudgeSerializer(context, typeMap); var bob = new Explicit.Sibling { Name = "Bob" }; var shirley = new Explicit.Sibling { Name = "Shirley" }; bob.Siblings.Add(shirley); // We don't reciprocate yet as that would generate a cycle var msg = serializer.SerializeToMsg(bob); var bob2 = (Explicit.Sibling)serializer.Deserialize(msg); Assert.NotSame(bob, bob2); Assert.Equal(1, bob2.Siblings.Count); Assert.NotSame(shirley, bob2.Siblings[0]); Assert.Equal("Shirley", bob2.Siblings[0].Name); }
public void ObjectIdentityNotEquals_FRN65() { // Using GetHashCode and Equals is not good enough for testing object identity // FRN65Class always returns true for Equals and a constant for GetHashCode var obj1 = new FRN65Class { Val = "A", Other = new FRN65Class { Val = "B" } }; var serializer = new FudgeSerializer(context); var msg = serializer.SerializeToMsg(obj1); var obj2 = (FRN65Class)serializer.Deserialize(msg); Assert.NotSame(obj2, obj2.Other); }
public void MessagesInObjectsOK() { // Case here is where a reference may be thrown out by other fields with messages in that aren't deserialized var obj1 = new ClassWithMessageIn(); var obj2 = new ClassWithMessageIn(); obj2.Message = new FudgeMsg(new Field("a", new Field("b"), new Field("c"))); // Add in an arbitrary message obj1.Other = obj2; obj2.Other = obj1; // We create a cycle so obj2 will refer back to obj1 past the other embedded messages var serializer = new FudgeSerializer(context); var msg = serializer.SerializeToMsg(obj1); var result = (ClassWithMessageIn)serializer.Deserialize(msg); Assert.NotSame(result, result.Other); Assert.Same(result, result.Other.Other); }
public void InlineObject() { var typeMap = new SerializationTypeMap(context); typeMap.RegisterType(typeof(Explicit.Person)); typeMap.RegisterType(typeof(Explicit.Address), new Explicit.AddressSerializer()); var serializer = new FudgeSerializer(context, typeMap); var person = new Explicit.Person { Name = "Bob", MainAddress = new Explicit.Address("Foo", "Bar", null) }; var msg = serializer.SerializeToMsg(person); var person2 = (Explicit.Person)serializer.Deserialize(msg); Assert.NotSame(person.MainAddress, person2.MainAddress); Assert.Equal(person.MainAddress.Line1, person2.MainAddress.Line1); }
public void NullHandling() { var obj1 = new ClassWithInner(); // Not setting the inner var serializer = new FudgeSerializer(context); var msg = serializer.SerializeToMsg(obj1); var obj2 = (ClassWithInner)serializer.Deserialize(msg); Assert.NotNull(obj2); Assert.Null(obj2.Inner); }
public void SecondaryTypes() { Assert.True(PropertyBasedSerializationSurrogate.CanHandle(typeDataCache, FudgeFieldNameConvention.Identity, typeof(SecondaryTypeClass))); var serializer = new FudgeSerializer(context); // We're relying on it auto-discovering the type surrogate var obj1 = new SecondaryTypeClass { Id = Guid.NewGuid() }; var msg = serializer.SerializeToMsg(obj1); var obj2 = (SecondaryTypeClass)serializer.Deserialize(msg); Assert.NotSame(obj1, obj2); Assert.Equal(obj1.Id, obj2.Id); }
public void SimpleExample() { Assert.True(PropertyBasedSerializationSurrogate.CanHandle(typeDataCache, FudgeFieldNameConvention.Identity, typeof(SimpleExampleClass))); var serializer = new FudgeSerializer(context); // We're relying on it auto-discovering the type surrogate var obj1 = new SimpleExampleClass { Name = "Dennis", Age = 37 }; var msg = serializer.SerializeToMsg(obj1); var obj2 = (SimpleExampleClass)serializer.Deserialize(msg); Assert.NotSame(obj1, obj2); Assert.Equal(obj1.Name, obj2.Name); Assert.Equal(obj1.Age, obj2.Age); }
public void StaticAndTransient() { Assert.True(PropertyBasedSerializationSurrogate.CanHandle(typeDataCache, FudgeFieldNameConvention.Identity, typeof(StaticTransientClass))); var serializer = new FudgeSerializer(context); // We're relying on it auto-discovering the type surrogate StaticTransientClass.Static = 17; var obj1 = new StaticTransientClass {Transient = "Hello"}; var msg = serializer.SerializeToMsg(obj1); StaticTransientClass.Static = 19; var obj2 = (StaticTransientClass)serializer.Deserialize(msg); Assert.NotSame(obj1, obj2); Assert.Equal(null, obj2.Transient); Assert.Equal(19, StaticTransientClass.Static); }
public void SimpleCase() { var obj1 = new SimpleTestClass { Val = "Test" }; var serializer = new FudgeSerializer(context); var msg = serializer.SerializeToMsg(obj1); Assert.Equal("Test", msg.GetString("serializedVal")); var obj2 = (SimpleTestClass)serializer.Deserialize(msg); Assert.Equal("Test", obj2.Val); }
public void SimpleExampleWithSurrogate() { var typeMap = new SerializationTypeMap(context); typeMap.RegisterType(typeof(Explicit.Address), new Explicit.AddressSerializer()); var serializer = new FudgeSerializer(context, typeMap); var address = new Explicit.Address("Our House", "In the middle of our street", "MD1"); var msg = serializer.SerializeToMsg(address); var address2 = (Explicit.Address)serializer.Deserialize(msg); Assert.Equal(address.Line1, address2.Line1); Assert.Equal(address.Line2, address2.Line2); Assert.Equal(address.Zip, address2.Zip); }
public void RenamingFields() { Assert.True(PropertyBasedSerializationSurrogate.CanHandle(typeDataCache, FudgeFieldNameConvention.Identity, typeof(RenameFieldClass))); var serializer = new FudgeSerializer(context); // We're relying on it auto-discovering the type surrogate var obj1 = new RenameFieldClass { Name = "Albert", Age = 72 }; var msg = serializer.SerializeToMsg(obj1); Assert.Null(msg.GetString("Name")); Assert.Equal("Albert", msg.GetString("nom")); Assert.Equal(72, msg.GetInt("Age")); var obj2 = (RenameFieldClass)serializer.Deserialize(msg); Assert.NotSame(obj1, obj2); Assert.Equal(obj1.Name, obj2.Name); Assert.Equal(obj1.Age, obj2.Age); }
public void PrimitiveLists() { Assert.True(PropertyBasedSerializationSurrogate.CanHandle(typeDataCache, FudgeFieldNameConvention.Identity, typeof(PrimitiveListClass))); var serializer = new FudgeSerializer(context); // We're relying on it auto-discovering the type surrogate var obj1 = new PrimitiveListClass(); obj1.Names.Add("Fred"); obj1.Names.Add("Sheila"); var msg = serializer.SerializeToMsg(obj1); // Check the serialized format Assert.Equal(FudgeMsgFieldType.Instance, msg.GetByName("Names").Type); var listMsg = msg.GetMessage("Names"); Assert.Equal("FudgeMsg[ => Fred, => Sheila]", listMsg.ToString()); var obj2 = (PrimitiveListClass)serializer.Deserialize(msg); Assert.NotSame(obj1, obj2); Assert.Equal(obj1.Names, obj2.Names); }
public void TryOutSomeTypes() { var obj1 = new ClassWithSomeTypes { Array = new int[] { 7, 3, -2 }, DateTime = DateTime.Now, List = new List<string>(), String = "Str" }; obj1.List.Add("a"); obj1.List.Add("b"); var serializer = new FudgeSerializer(context); var msg = serializer.SerializeToMsg(obj1); var obj2 = (ClassWithSomeTypes)serializer.Deserialize(msg); Assert.Equal(obj1.Array, obj2.Array); // Times are deserialized into UTC, so need to convert the source for comparison Assert.Equal(obj1.DateTime.ToUniversalTime(), obj2.DateTime); Assert.Equal(obj1.List, obj2.List); Assert.Equal(obj1.String, obj2.String); }
public void InlineAttribute_FRN48() { var serializer = new FudgeSerializer(context); var parent = new InlineParent(); parent.SetUp(); var msg = serializer.SerializeToMsg(parent); Assert.Equal(FudgeMsgFieldType.Instance, msg.GetByName("In1").Type); Assert.Equal(FudgeMsgFieldType.Instance, msg.GetByName("In2").Type); Assert.Equal(PrimitiveFieldTypes.SByteType, msg.GetByName("In1ForcedOut").Type); // References In1 and collapses to byte Assert.Equal(FudgeMsgFieldType.Instance, msg.GetByName("Out1").Type); Assert.Equal(PrimitiveFieldTypes.SByteType, msg.GetByName("Out2").Type); // References Out1 Assert.Equal(FudgeMsgFieldType.Instance, msg.GetByName("Out2ForcedIn").Type); }
public void Dictionaries() { Assert.True(PropertyBasedSerializationSurrogate.CanHandle(typeDataCache, FudgeFieldNameConvention.Identity, typeof(DictionaryClass))); var serializer = new FudgeSerializer(context); // We're relying on it auto-discovering the type surrogate var obj1 = new DictionaryClass(); obj1.Map = new Dictionary<string, SimpleExampleClass>(); obj1.Map["Fred"] = new SimpleExampleClass { Name = "Fred", Age = 23 }; obj1.Map["Jemima"] = new SimpleExampleClass { Name = "Jemima", Age = 17 }; var msg = serializer.SerializeToMsg(obj1); var obj2 = (DictionaryClass)serializer.Deserialize(msg); Assert.NotSame(obj1, obj2); Assert.NotSame(obj1.Map, obj2.Map); Assert.Equal(obj1.Map["Fred"], obj2.Map["Fred"]); Assert.Equal(obj1.Map["Jemima"], obj2.Map["Jemima"]); }