public void CheckElementsCorrectForSubMessage()
        {
            var context = new FudgeContext();
            var msg     = context.NewMessage();
            var subMsg  = context.NewMessage();

            msg.Add("sub", subMsg);
            subMsg.Add("Test", "Bob");
            var bytes = context.ToByteArray(msg);

            var stream = new MemoryStream(bytes);
            var reader = new FudgeEncodedStreamReader(context, stream);

            Assert.True(reader.HasNext);
            Assert.Equal(FudgeStreamElement.MessageStart, reader.MoveNext());
            Assert.True(reader.HasNext);
            Assert.Equal(FudgeStreamElement.SubmessageFieldStart, reader.MoveNext());
            Assert.True(reader.HasNext);
            Assert.Equal(FudgeStreamElement.SimpleField, reader.MoveNext());
            Assert.True(reader.HasNext);
            Assert.Equal(FudgeStreamElement.SubmessageFieldEnd, reader.MoveNext());
            Assert.True(reader.HasNext);
            Assert.Equal(FudgeStreamElement.MessageEnd, reader.MoveNext());
            Assert.False(reader.HasNext);
        }
        public void MultipleMessages()
        {
            // Same as CheckElementsCorrectForSimpleMessage but without using HasNext
            var context = new FudgeContext();
            var msg1    = context.NewMessage();

            msg1.Add("Test", "Bob");
            var msg2 = context.NewMessage();

            msg2.Add("Test2", "Shirley");
            var msgs   = new FudgeMsg[] { msg1, msg2 };
            var stream = new MemoryStream();
            var writer = new FudgeEncodedStreamWriter(context, stream);

            new FudgeStreamPipe(new FudgeMsgStreamReader(context, msgs), writer).Process();

            stream.Position = 0;
            var reader = new FudgeEncodedStreamReader(context, stream);

            Assert.Equal(FudgeStreamElement.MessageStart, reader.MoveNext());
            Assert.Equal(FudgeStreamElement.SimpleField, reader.MoveNext());
            Assert.Equal(FudgeStreamElement.MessageEnd, reader.MoveNext());
            Assert.Equal(FudgeStreamElement.MessageStart, reader.MoveNext());
            Assert.Equal(FudgeStreamElement.SimpleField, reader.MoveNext());
            Assert.Equal(FudgeStreamElement.MessageEnd, reader.MoveNext());
            Assert.Equal(FudgeStreamElement.NoElement, reader.MoveNext());
            Assert.Equal(FudgeStreamElement.NoElement, reader.MoveNext());
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 4
0
        public static void CycleInputToOutput(string typeHint)
        {
            var openGammaFudgeContext = new OpenGammaFudgeContext();
            var fudgeSerializer       = openGammaFudgeContext.GetSerializer();

            using (var openStandardInput = Console.OpenStandardInput())
                using (var openStandardOutput = Console.OpenStandardOutput())
                {
                    var fudgeEncodedStreamWriter = new FudgeEncodedStreamWriter(openGammaFudgeContext, openStandardOutput);
                    var fudgeEncodedStreamReader = new FudgeEncodedStreamReader(openGammaFudgeContext, openStandardInput);

                    var mappingStrategy = (IFudgeTypeMappingStrategy)openGammaFudgeContext.GetProperty(ContextProperties.TypeMappingStrategyProperty);

                    var mappedtype = mappingStrategy.GetType(typeHint) ?? typeof(object);

                    var hintType = typeof(TestWrapper <>).MakeGenericType(mappedtype);

                    var wrapper = (ITestWrapper)Deserialize(fudgeSerializer, hintType, fudgeEncodedStreamReader);

                    object hydratedObject = wrapper.TestObject;
                    CheckEqualityIfAppropriate(hydratedObject, mappedtype);

                    fudgeSerializer.Serialize(fudgeEncodedStreamWriter, wrapper);

                    openStandardOutput.Flush();
                }
        }
Ejemplo n.º 5
0
        public T DeFudgeSerialize <T>(Stream stream)
        {
            T t;

            if (_streamingBuilder.TryDeserialize(stream, out t))
            {
                return(t);
            }

            var fudgeEncodedStreamReader = new FudgeEncodedStreamReader(this, stream);

            return(GetSerializer().Deserialize <T>(fudgeEncodedStreamReader));
        }
        public void CheckEndOfStreamWithoutHasNext()
        {
            // Same as CheckElementsCorrectForSimpleMessage but without using HasNext
            var context = new FudgeContext();
            var msg     = context.NewMessage();

            msg.Add("Test", "Bob");
            var bytes = context.ToByteArray(msg);

            var stream = new MemoryStream(bytes);
            var reader = new FudgeEncodedStreamReader(context, stream);

            Assert.Equal(FudgeStreamElement.MessageStart, reader.MoveNext());
            Assert.Equal(FudgeStreamElement.SimpleField, reader.MoveNext());
            Assert.Equal(FudgeStreamElement.MessageEnd, reader.MoveNext());
            Assert.Equal(FudgeStreamElement.NoElement, reader.MoveNext());
            Assert.Equal(FudgeStreamElement.NoElement, reader.MoveNext());
        }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Parses data from a given <see cref="BinaryReader"/>.
        /// </summary>
        /// <param name="binaryReader"></param>
        /// <returns></returns>
        public FudgeMsgEnvelope Parse(BinaryReader binaryReader)
        {
            FudgeEncodedStreamReader reader = new FudgeEncodedStreamReader(FudgeContext);

            reader.Reset(binaryReader);
            FudgeStreamElement element = reader.MoveNext();

            if (element == FudgeStreamElement.NoElement)
            {
                return(null);
            }
            if (element != FudgeStreamElement.MessageStart)
            {
                throw new ArgumentException("First element in encoding stream wasn't a message element.");
            }
            int              version  = reader.SchemaVersion;
            FudgeMsg         msg      = FudgeContext.NewMessage();
            FudgeMsgEnvelope envelope = new FudgeMsgEnvelope(msg, version);

            ProcessFields(reader, msg);
            return(envelope);
        }
Ejemplo n.º 9
0
        private static object Deserialize(FudgeSerializer fudgeSerializer, Type mappedtype, FudgeEncodedStreamReader fudgeEncodedStreamReader)
        {
            var methodInfo = fudgeSerializer.GetType().GetMethods().Where(
                m => m.Name == "Deserialize" &&
                m.GetParameters().Count() == 1 && m.GetParameters().Single().ParameterType.IsAssignableFrom(fudgeEncodedStreamReader.GetType()) &&
                m.ContainsGenericParameters
                ).Select(m => m.MakeGenericMethod(new[] { mappedtype })).Single();

            return(methodInfo.Invoke(fudgeSerializer, new object[] { fudgeEncodedStreamReader }));
        }