Exemple #1
0
        private static void CheckRoundTrip <TFirst, TSecond>(FudgeSerializer fudgeSerializer, Pair <TFirst, TSecond> orig, FudgeMsg message)
        {
            var genericRound = fudgeSerializer.Deserialize <Pair <TFirst, TSecond> >(message);

            Assert.Equal(orig.First, genericRound.First);
            Assert.Equal(orig.Second, genericRound.Second);

            var nonGenericRound = (Pair <TFirst, TSecond>)fudgeSerializer.Deserialize <Pair>(message);

            Assert.Equal(orig.First, nonGenericRound.First);
            Assert.Equal(orig.Second, nonGenericRound.Second);
        }
        public void ConstructorOrderRelevant()
        {
            var msg1 = context.NewMessage(new Field(FudgeSerializer.TypeIdFieldOrdinal, typeof(SimpleClass).ToString()),
                                          new Field("Val1", "Test"),
                                          new Field("Val2", "Case"));
            var msg2 = context.NewMessage(new Field(FudgeSerializer.TypeIdFieldOrdinal, typeof(SimpleClass).ToString()),
                                          new Field("Val2", "Case"),
                                          new Field("Val1", "Test"));
            var serializer = new FudgeSerializer(context);

            var obj1 = (SimpleClass)serializer.Deserialize(msg1);
            var obj2 = (SimpleClass)serializer.Deserialize(msg2);
            Assert.Equal(obj1.Val1, obj2.Val1);
            Assert.Equal(obj1.Val2, obj2.Val2);
        }
Exemple #3
0
        public void VerySimpleExample()
        {
            // This code is used as the example in the NamespaceDoc for Fudge.Serialization

            // Create a context and a serializer
            var context    = new FudgeContext();
            var serializer = new FudgeSerializer(context);

            // Our object to serialize
            var temperatureRange = new TemperatureRange {
                High = 28.3, Low = 13.2, Average = 19.6
            };

            // Serialize it to a MemoryStream
            var stream       = new MemoryStream();
            var streamWriter = new FudgeEncodedStreamWriter(context, stream);

            serializer.Serialize(streamWriter, temperatureRange);

            // Reset the stream and deserialize a new object from it
            stream.Position = 0;
            var streamReader = new FudgeEncodedStreamReader(context, stream);
            var range2       = (TemperatureRange)serializer.Deserialize(streamReader);

            // Just check a value matches
            Assert.Equal(temperatureRange, range2);
        }
Exemple #4
0
        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);
        }
Exemple #5
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);
        }
Exemple #6
0
        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]);
        }
Exemple #7
0
        public void ConstructorOrderRelevant()
        {
            var msg1 = context.NewMessage(new Field(FudgeSerializer.TypeIdFieldOrdinal, typeof(SimpleClass).ToString()),
                                          new Field("Val1", "Test"),
                                          new Field("Val2", "Case"));
            var msg2 = context.NewMessage(new Field(FudgeSerializer.TypeIdFieldOrdinal, typeof(SimpleClass).ToString()),
                                          new Field("Val2", "Case"),
                                          new Field("Val1", "Test"));
            var serializer = new FudgeSerializer(context);

            var obj1 = (SimpleClass)serializer.Deserialize(msg1);
            var obj2 = (SimpleClass)serializer.Deserialize(msg2);

            Assert.Equal(obj1.Val1, obj2.Val1);
            Assert.Equal(obj1.Val2, obj2.Val2);
        }
Exemple #8
0
        protected T1 DeserializeStandard <T1>(OpenGammaFudgeContext context, IFudgeStreamReader reader, SerializationTypeMap typeMap)
        {
            //Called just after SubmessageFieldStart for a field containing a T1
            //TODO: should handle T1 being stream serializable
            FudgeMsg dequeueMessage  = ReadOneSubmessage(context, reader);
            var      fudgeSerializer = new FudgeSerializer(context, typeMap);

            return(fudgeSerializer.Deserialize <T1>(dequeueMessage));
        }
 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);
 }
Exemple #10
0
        public void MissingFieldsAreNull()
        {
            var msg1 = context.NewMessage(new Field(FudgeSerializer.TypeIdFieldOrdinal, typeof(SimpleClass).ToString()),
                                          new Field("Val2", "Case"));
            var serializer = new FudgeSerializer(context);
            var obj1       = (SimpleClass)serializer.Deserialize(msg1);

            Assert.Null(obj1.Val1);
            Assert.Equal("Case", obj1.Val2);
        }
Exemple #11
0
        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 ChoosesRightConstructor()
 {
     var msg1 = context.NewMessage(new Field(FudgeSerializer.TypeIdFieldOrdinal, typeof(MultiConstructor).ToString()),
                                   new Field("A", 17),
                                   new Field("B", "foo"));
     var serializer = new FudgeSerializer(context);
     var obj1 = (MultiConstructor)serializer.Deserialize(msg1);
     Assert.Equal(17, obj1.A);
     Assert.Equal("foo", obj1.B);
 }
Exemple #13
0
        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);
        }
Exemple #14
0
        public void ChoosesRightConstructor()
        {
            var msg1 = context.NewMessage(new Field(FudgeSerializer.TypeIdFieldOrdinal, typeof(MultiConstructor).ToString()),
                                          new Field("A", 17),
                                          new Field("B", "foo"));
            var serializer = new FudgeSerializer(context);
            var obj1       = (MultiConstructor)serializer.Deserialize(msg1);

            Assert.Equal(17, obj1.A);
            Assert.Equal("foo", obj1.B);
        }
Exemple #15
0
        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 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);
        }
Exemple #17
0
        public void UsesFirstKnownType_FRN43()
        {
            var serializer = new FudgeSerializer(context);

            var msg = context.NewMessage(new Field(0, "Bibble"),
                                         new Field(0, "Fudge.Tests.Unit.Serialization.Explicit+Sibling"),
                                         new Field("name", "Bob"));
            var bob = (Explicit.Sibling)serializer.Deserialize(msg);

            Assert.Equal("Bob", bob.Name);
        }
        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);
        }
Exemple #19
0
        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);
        }
Exemple #20
0
        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 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 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);
        }
Exemple #23
0
        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);
        }
Exemple #24
0
        private TRet ProjectSubMessage <TRet>(FudgeMsg retMsg, string subMessageField)
        {
            if (retMsg == null)
            {
                return(default(TRet));
            }
            FudgeSerializer fudgeSerializer = _fudgeContext.GetSerializer();
            var             subMessage      = retMsg.GetMessage(subMessageField);

            if (subMessage == null)
            {
                return(default(TRet));
            }
            return(fudgeSerializer.Deserialize <TRet>((FudgeMsg)subMessage));
        }
Exemple #25
0
        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 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);
        }
Exemple #27
0
        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 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);
        }
Exemple #29
0
        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 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 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);
        }
Exemple #33
0
        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 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));
        }
Exemple #35
0
        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 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]);
        }
Exemple #37
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 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 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 VerySimpleExample()
        {
            // This code is used as the example in the NamespaceDoc for Fudge.Serialization

            // Create a context and a serializer
            var context = new FudgeContext();
            var serializer = new FudgeSerializer(context);

            // Our object to serialize
            var temperatureRange = new TemperatureRange { High = 28.3, Low = 13.2, Average = 19.6 };

            // Serialize it to a MemoryStream
            var stream = new MemoryStream();
            var streamWriter = new FudgeEncodedStreamWriter(context, stream);
            serializer.Serialize(streamWriter, temperatureRange);

            // Reset the stream and deserialize a new object from it
            stream.Position = 0;
            var streamReader = new FudgeEncodedStreamReader(context, stream);
            var range2 = (TemperatureRange)serializer.Deserialize(streamReader);

            // Just check a value matches
            Debug.Assert(range2.Average == 19.6);
        }
        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 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 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 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 RecursiveDataType_TypeReferences()
        {
            //
            var context = new FudgeContext();
            var serializer = new FudgeSerializer(context);

            var obj = new RecursiveDataType
            {
                Text = "SomeString",
                TemperatureRange = new TemperatureRange { Low = 18, High = 24, Average = 21 },
                Inner = new RecursiveDataType
                            {
                                Text = "SomeOtherString",
                                TemperatureRange = new TemperatureRange { Low = 5, High = 15, Average = 10 },
                                Inner = new RecursiveDataType
                                    {
                                        Text = "AnotherString",
                                        TemperatureRange = new TemperatureRange { Low = 35, High = 40, Average = 37.5 },
                                        Inner = new RecursiveDataType()
                                    }
                            }
            };

            // Serialize it to a MemoryStream
            var stream = new MemoryStream();
            var streamWriter = new FudgeEncodedStreamWriter(context, stream);
            serializer.Serialize(streamWriter, obj);

            // Reset the stream and deserialize a new object from it
            stream.Position = 0;
            var streamReader = new FudgeEncodedStreamReader(context, stream);
            var cycledObj = (RecursiveDataType)serializer.Deserialize(streamReader);

            // Just check a value matches
            Assert.Equal(obj, cycledObj);
        }
        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 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 UsesFirstKnownType_FRN43()
        {
            var serializer = new FudgeSerializer(context);

            var msg = context.NewMessage(new Field(0, "Bibble"),
                                         new Field(0, "Fudge.Tests.Unit.Serialization.Explicit+Sibling"),
                                         new Field("name", "Bob"));
            var bob = (Explicit.Sibling)serializer.Deserialize(msg);
            Assert.Equal("Bob", bob.Name);
        }
        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 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"]);
        }
        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);
        }