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 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 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);
        }
Example #4
0
 /// <summary>
 /// Encodes a <c>FudgeMsg</c> object to a <c>Stream</c> with an optional taxonomy reference.
 /// If a taxonomy is supplied it may be used to optimize the output by writing ordinals instead
 /// of field names.
 /// </summary>
 /// <param name="msg">the <c>FudgeMessage</c> to write</param>
 /// <param name="taxonomyId">the identifier of the taxonomy to use. Specify <c>null</c> for no taxonomy</param>
 /// <param name="bw">The <see cref="BinaryWriter"/> to serialise to</param>
 public void Serialize(FudgeMsg msg, short? taxonomyId, BinaryWriter bw)
 {
     try
     {
         var writer = new FudgeEncodedStreamWriter(this);
         writer.TaxonomyId = taxonomyId;
         writer.Reset(bw);
         writer.StartMessage();
         writer.WriteFields(msg.GetAllFields());
         writer.EndMessage();
     }
     catch (IOException e)
     {
         throw new FudgeRuntimeException("Unable to write Fudge message to OutputStream", e);
     }
 }