internal static FudgeMsg CreateMessageAllNames(FudgeContext context) { FudgeMsg msg = context.NewMessage(); msg.Add("boolean", true); msg.Add("Boolean", (object)false); msg.Add("byte", (sbyte)5); msg.Add("Byte", (object)((sbyte)5)); short shortValue = ((short)sbyte.MaxValue) + 5; msg.Add("short", shortValue); msg.Add("Short", (object)(shortValue)); int intValue = ((int)short.MaxValue) + 5; msg.Add("int", intValue); msg.Add("Integer", (object)(intValue)); long longValue = ((long)int.MaxValue) + 5; msg.Add("long", longValue); msg.Add("Long", (object)(longValue)); msg.Add("float", 0.5f); msg.Add("Float", (object)(0.5f)); msg.Add("double", 0.27362); msg.Add("Double", (object)(0.27362)); msg.Add("String", "Kirk Wylie"); msg.Add("float array", new float[24]); msg.Add("double array", new double[273]); msg.Add("short array", new short[32]); msg.Add("int array", new int[83]); msg.Add("long array", new long[837]); msg.Add("indicator", IndicatorType.Instance); return msg; }
public void RaisesEventAfterEachMessage() { var context = new FudgeContext(); var messages = new FudgeMsg[] { new FudgeMsg(context, new Field("test", "data")), new FudgeMsg(context, new Field("life", 42)) }; var reader = new FudgeMsgStreamReader(context, messages); var writer = new FudgeMsgStreamWriter(context); var pipe = new FudgeStreamPipe(reader, writer); int count = 0; pipe.MessageProcessed += () => { var message = writer.DequeueMessage(); FudgeUtils.AssertAllFieldsMatch(messages[count], message); count++; }; pipe.Process(); Assert.Equal(2, count); }
/// <summary> /// Constructs a new instance for a specific type /// </summary> /// <param name="context"><see cref="FudgeContext"/> for this surrogate.</param> /// <param name="typeData"><see cref="TypeData"/> describing the type to serialize.</param> public DataContractSurrogate(FudgeContext context, TypeData typeData) { if (context == null) { throw new ArgumentNullException("context"); } if (typeData == null) { throw new ArgumentNullException("typeData"); } if (!CanHandle(typeData)) { throw new ArgumentOutOfRangeException("typeData", "ImmutableSurrogate cannot handle " + typeData.Type.FullName); } this.context = context; this.type = typeData.Type; Debug.Assert(typeData.DefaultConstructor != null); // Should have been caught in CanHandle() var properties = from prop in typeData.Properties.Concat(typeData.Fields) where prop.GetCustomAttribute <DataMemberAttribute>() != null select prop; this.helper = new PropertyBasedSerializationSurrogate.PropertySerializerMixin(context, typeData, properties, new DotNetSerializableSurrogate.BeforeAfterMethodMixin(context, typeData)); }
/// <summary> /// Constructs a new <see cref="SerializableAttributeSurrogate"/>. /// </summary> /// <param name="context"><see cref="FudgeContext"/> to use.</param> /// <param name="typeData"><see cref="TypeData"/> for the type for this surrogate.</param> public SerializableAttributeSurrogate(FudgeContext context, TypeData typeData) { if (context == null) { throw new ArgumentNullException("context"); } if (typeData == null) { throw new ArgumentNullException("typeData"); } if (!CanHandle(typeData)) { throw new ArgumentOutOfRangeException("typeData", "SerializableAttributeSurrogate cannot handle " + typeData.Type.FullName); } this.type = typeData.Type; var fields = from field in typeData.Fields where field.GetCustomAttribute <NonSerializedAttribute>() == null select field; var beforeAfterMixin = new DotNetSerializableSurrogate.BeforeAfterMethodMixin(context, typeData); this.serializerMixin = new PropertyBasedSerializationSurrogate.PropertySerializerMixin(context, typeData, fields, beforeAfterMixin); }
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); }
internal static FudgeMsg CreateMessageAllOrdinals(FudgeContext context) { FudgeMsg msg = context.NewMessage(); msg.Add(1, true); msg.Add(2, (object)(false)); msg.Add(3, (sbyte)5); msg.Add(4, (object)((sbyte)5)); short shortValue = ((short)sbyte.MaxValue) + 5; msg.Add(5, shortValue); msg.Add(6, (object)(shortValue)); int intValue = ((int)short.MaxValue) + 5; msg.Add(7, intValue); msg.Add(8, (object)(intValue)); long longValue = ((long)int.MaxValue) + 5; msg.Add(9, longValue); msg.Add(10, (object)(longValue)); msg.Add(11, 0.5f); msg.Add(12, (object)(0.5f)); msg.Add(13, 0.27362); msg.Add(14, (object)(0.27362)); msg.Add(15, "Kirk Wylie"); msg.Add(16, new float[24]); msg.Add(17, new double[273]); return(msg); }
/// <summary> /// Constructs a new instance /// </summary> /// <param name="context"></param> /// <param name="cache"></param> /// <param name="type"></param> /// <param name="fieldNameConvention"></param> public TypeData(FudgeContext context, TypeDataCache cache, Type type, FudgeFieldNameConvention fieldNameConvention) { Type = type; DefaultConstructor = type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, Type.EmptyTypes, null); Constructors = type.GetConstructors(); CustomAttributes = type.GetCustomAttributes(true); cache.RegisterTypeData(this); fieldNameConvention = OverrideFieldNameConvention(type, fieldNameConvention); var kind = CalcKind(context, cache, type, fieldNameConvention, out subType, out subType2, out fieldType); var inlineAttrib = GetCustomAttribute <FudgeInlineAttribute>(); if (inlineAttrib != null) { kind = inlineAttrib.Inline ? TypeKind.Inline : TypeKind.Reference; } Kind = kind; if (kind != TypeKind.FudgePrimitive) // If it's primitive we won't need to look inside it to serialize it { ScanProperties(context, cache, fieldNameConvention); ScanFields(context, cache, fieldNameConvention); PublicMethods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance); AllInstanceMethods = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); StaticPublicMethods = type.GetMethods(BindingFlags.Public | BindingFlags.Static); } }
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 AllOrdinalsCodecWithTaxonomy() { FudgeContext context = new FudgeContext(); FudgeMsg inputMsg = context.NewMessage(); inputMsg.Add(ORDINALS[0], "value1"); inputMsg.Add(ORDINALS[1], "value2"); inputMsg.Add(ORDINALS[2], "value3"); inputMsg.Add(ORDINALS[3], "value4"); var resolverMap = new Dictionary <int, IFudgeTaxonomy>(); resolverMap.Add(45, new MapFudgeTaxonomy(ORDINALS, NAMES)); context.TaxonomyResolver = new ImmutableMapTaxonomyResolver(resolverMap); FudgeMsg outputMsg = CycleMessage(inputMsg, context, (short)45); Assert.Equal("value1", outputMsg.GetString(NAMES[0])); Assert.Equal("value1", outputMsg.GetString(ORDINALS[0])); Assert.Equal("value2", outputMsg.GetString(NAMES[1])); Assert.Equal("value2", outputMsg.GetString(ORDINALS[1])); Assert.Equal("value3", outputMsg.GetString(NAMES[2])); Assert.Equal("value3", outputMsg.GetString(ORDINALS[2])); Assert.Equal("value4", outputMsg.GetString(NAMES[3])); Assert.Equal("value4", outputMsg.GetString(ORDINALS[3])); }
public void Example() { var context = new FudgeContext(); // Create a message var msg = new FudgeMsg(new Field("name", "Eric"), new Field("age", 14), new Field("address", new Field("line1", "29 Acacia Road"), new Field("city", "London"))); // Serialise it var stream = new MemoryStream(); context.Serialize(msg, stream); // Get the raw bytes var bytes = stream.ToArray(); // Deserialise it var msg2 = context.Deserialize(bytes).Message; // Get some data int age = msg2.GetInt("age") ?? 0; }
public FudgeSerializationContext(FudgeContext context, SerializationTypeMap typeMap, IFudgeStreamWriter writer, IFudgeTypeMappingStrategy typeMappingStrategy) { this.context = context; this.writer = writer; this.typeMap = typeMap; this.typeMappingStrategy = typeMappingStrategy; }
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()); }
protected FudgeMsgEnvelope CycleMessage(FudgeContext context, FudgeMsg msg) { FudgeStreamParser parser = new FudgeStreamParser(context); byte[] msgAsBytes = context.ToByteArray(msg); return(parser.Parse(new MemoryStream(msgAsBytes))); }
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); }
/// <summary> /// Constructs a new <see cref="SerializationTypeMap"/> /// </summary> /// <param name="context"></param> /// <param name="surrogateSelector"></param> public SerializationTypeMap(FudgeContext context, FudgeSurrogateSelector surrogateSelector) { this.context = context; this.surrogateSelector = surrogateSelector; this.AllowTypeDiscovery = (bool)context.GetProperty(ContextProperties.AllowTypeDiscoveryProperty, true); this.FieldNameConvention = (FudgeFieldNameConvention)context.GetProperty(ContextProperties.FieldNameConventionProperty, FudgeFieldNameConvention.Identity); RegisterSurrogateSelector((ISurrogateSelector)context.GetProperty(ContextProperties.DotNetSurrogateSelectorProperty)); }
/// <summary> /// Constructs a new <see cref="SerializationTypeMap"/> /// </summary> /// <param name="context"></param> public SerializationTypeMap(FudgeContext context) { this.context = context; this.surrogateSelector = new FudgeSurrogateSelector(context); this.AllowTypeDiscovery = (bool)context.GetProperty(ContextProperties.AllowTypeDiscoveryProperty, true); this.FieldNameConvention = (FudgeFieldNameConvention)context.GetProperty(ContextProperties.FieldNameConventionProperty, FudgeFieldNameConvention.Identity); RegisterSurrogateSelector((ISurrogateSelector)context.GetProperty(ContextProperties.DotNetSurrogateSelectorProperty)); }
public FudgeDeserializationContext(FudgeContext context, SerializationTypeMap typeMap, IFudgeTypeMappingStrategy typeMappingStrategy) { this.context = context; this.typeMap = typeMap; this.objectList = new List <MsgAndObj>(); this.stack = new Stack <State>(); this.typeMappingStrategy = typeMappingStrategy; }
/// <summary> /// Constructs a new instance. /// </summary> /// <param name="context"></param> /// <param name="typeData"></param> /// <param name="serializeMethodName">Name of method to use to serialize objects.</param> /// <param name="deserializeMethodName">Name of method to use to deserialize objects.</param> /// <remarks> /// The <see cref="CollectionSurrogateBase"/> will scan for generic methods with the right names, and specialise them for /// the types given within <see cref="typeData"/>. /// </remarks> public CollectionSurrogateBase(FudgeContext context, TypeData typeData, string serializeMethodName, string deserializeMethodName) { this.context = context; this.typeData = typeData; Type[] types = (typeData.SubType2 == null) ? new Type[] { typeData.SubType } : new Type[] { typeData.SubType, typeData.SubType2 }; serializerDelegate = ReflectionUtil.CreateInstanceMethodDelegate <Action <object, IAppendingFudgeFieldContainer, IFudgeSerializer> >(this, serializeMethodName, types); deserializerDelegate = ReflectionUtil.CreateInstanceMethodDelegate <Func <IFudgeFieldContainer, IFudgeDeserializer, object> >(this, deserializeMethodName, types); }
public void SetViewCycleAccessSupported(bool isViewCycleAccessSupported) { var msg = FudgeContext.NewMessage( new Field("isViewCycleAccessSupported", isViewCycleAccessSupported) ); REST.Resolve("viewCycleAccessSupported").PostFudge(msg); }
protected BuilderBase(FudgeContext context, Type type) { if (!(type.IsAssignableFrom(typeof(T)) || typeof(T).IsAssignableFrom(type))) { throw new ArgumentException("Type paramter doesn't match generic parameter", "type"); } Context = context; }
public void RangeChecking() { var context = new FudgeContext(); var cache = new TypeDataCache(context); Assert.Throws<ArgumentNullException>(() => new TypeDataCache(null)); Assert.Throws<ArgumentNullException>(() => cache.GetTypeData(null, FudgeFieldNameConvention.Identity)); }
public PropertySerializerMixin(FudgeContext context, TypeData typeData, IEnumerable <TypeData.PropertyData> properties, DotNetSerializableSurrogate.BeforeAfterMethodMixin beforeAfterMethodHelper) { this.context = context; this.typeData = typeData; this.beforeAfterMethodHelper = beforeAfterMethodHelper; ExtractProperties(properties); }
/// <summary> /// Constructs a new <see cref="FudgeEncodedStreamReader"/> with a given <see cref="FudgeContext"/>. /// </summary> /// <param name="fudgeContext"><see cref="FudgeContext"/> to use for messages read from the stream.</param> public FudgeEncodedStreamReader(FudgeContext fudgeContext) { if (fudgeContext == null) { throw new ArgumentNullException("fudgeContext"); } this.fudgeContext = fudgeContext; }
public void RangeChecking() { var context = new FudgeContext(); var cache = new TypeDataCache(context); Assert.Throws <ArgumentNullException>(() => new TypeDataCache(null)); Assert.Throws <ArgumentNullException>(() => cache.GetTypeData(null, FudgeFieldNameConvention.Identity)); }
/// <summary> /// Cosntructs a new <see cref="TypeDataCache"/>. /// </summary> /// <param name="context">Context for this cache.</param> public TypeDataCache(FudgeContext context) { if (context == null) { throw new ArgumentNullException("context"); } this.context = context; }
public void HandlesCycles() { var context = new FudgeContext(); var cache = new TypeDataCache(context); var data = cache.GetTypeData(typeof(Cycle), FudgeFieldNameConvention.Identity); Assert.NotNull(data); Assert.Equal(data, data.Properties[0].TypeData); }
/// <summary> /// Constructs a new <see cref="FudgeEncodedStreamWriter"/> using a given <see cref="FudgeContext"/>. /// </summary> /// <param name="context"><see cref="FudgeContext"/> to use to write messages.</param> public FudgeEncodedStreamWriter(FudgeContext context) { if (context == null) { throw new ArgumentNullException("context"); } this.context = context; this.copyBuffer = new byte[CopyBufferSize]; }
private void ScanFields(FudgeContext context, TypeDataCache cache, FudgeFieldNameConvention fieldNameConvention) { var fields = Type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); var list = from field in fields where field.GetCustomAttributes(typeof(FudgeTransientAttribute), true).Length == 0 select new PropertyData(cache, fieldNameConvention, field); Fields = list.ToArray(); }
internal static FudgeMsg CreateLargeMessage(FudgeContext context) { FudgeMsg msg = context.NewMessage(); for (int i = short.MinValue - 1; i <= short.MaxValue + 1; i++) { msg.Add(string.Format("field{0}", i), "FRN-91"); } return msg; }
/// <summary> /// Constructs a new <see cref="FudgeMsgStreamReader"/> using a set of <see cref="IFudgeFieldContainer"/>s for data. /// </summary> /// <param name="context">Context to control behaviours.</param> /// <param name="messages">Set <see cref="IFudgeFieldContainer"/>s to provide as a stream.</param> public FudgeMsgStreamReader(FudgeContext context, IEnumerable<IFudgeFieldContainer> messages) { if (context == null) throw new ArgumentNullException("context"); this.context = context; messageSource = messages.GetEnumerator(); currentState = null; }
/// <summary> /// Constructs a new <see cref="FudgeSurrogateSelector"/>. /// </summary> /// <param name="context"><see cref="FudgeContext"/> for this selector.</param> public FudgeSurrogateSelector(FudgeContext context) { if (context == null) throw new ArgumentNullException("context"); this.context = context; this.typeDataCache = new TypeDataCache(context); this.selectors = BuildSelectorList(); }
public void PropertiesCreatedAfterContext() { var context = new FudgeContext(); var newProp = new FudgeContextProperty("NewProp"); Assert.Null(context.GetProperty(newProp)); context.SetProperty(newProp, "test"); Assert.Equal("test", context.GetProperty(newProp)); }
/// <summary> /// Constructs a new <see cref="FudgeEncodedStreamReader"/> with a given <see cref="FudgeContext"/> reading from a specified <see cref="BinaryReader"/>. /// </summary> /// <param name="fudgeContext"><see cref="FudgeContext"/> to use for messages read from the stream.</param> /// <param name="reader"><see cref="BinaryReader"/> to read the binary data from.</param> public FudgeEncodedStreamReader(FudgeContext fudgeContext, BinaryReader reader) : this(fudgeContext) { if (reader == null) { throw new ArgumentNullException("reader", "Must provide a BinaryReader to consume data from."); } this.reader = reader; CurrentElement = FudgeStreamElement.NoElement; }
private IFudgeSerializationSurrogate SurrogateFromAttribute(FudgeContext context, TypeData typeData) { var surrogateAttribute = typeData.CustomAttributes.FirstOrDefault(attrib => attrib is FudgeSurrogateAttribute); if (surrogateAttribute != null) { return(BuildSurrogate(typeData.Type, (FudgeSurrogateAttribute)surrogateAttribute)); } return(null); }
private void ScanProperties(FudgeContext context, TypeDataCache cache, FudgeFieldNameConvention fieldNameConvention) { var props = Type.GetProperties(BindingFlags.Public | BindingFlags.Instance); var list = from prop in props where prop.GetIndexParameters().Length == 0 && // We don't want to deal with anything with indices - e.g. this[string] prop.GetCustomAttributes(typeof(FudgeTransientAttribute), true).Length == 0 select new PropertyData(cache, fieldNameConvention, prop); Properties = list.ToArray(); }
/// <summary> /// Constructs a new <c>FudgeXmlStreamReader</c> using a given <see cref="XmlReader"/> as the source of the XML data. /// </summary> /// <param name="context">Context to control behaviours.</param> /// <param name="reader"><see cref="XmlReader"/> providing the XML data</param> public FudgeXmlStreamReader(FudgeContext context, XmlReader reader) { if (context == null) throw new ArgumentNullException("context"); if (reader == null) throw new ArgumentNullException("reader"); this.context = context; this.reader = reader; }
public void AllNamesCodecNoTaxonomy() { FudgeMsg inputMsg = StandardFudgeMessages.CreateMessageAllNames(fudgeContext); FudgeContext context = new FudgeContext(); FudgeMsg outputMsg = CycleMessage(inputMsg, context, null); Assert.NotNull(outputMsg); FudgeUtils.AssertAllFieldsMatch(inputMsg, outputMsg); }
internal static FudgeMsg CreateLargeMessage(FudgeContext context) { FudgeMsg msg = context.NewMessage(); for (int i = short.MinValue - 1; i <= short.MaxValue + 1; i++) { msg.Add(string.Format("field{0}", i), "FRN-91"); } return(msg); }
/** * @param msg */ protected void CheckResultsMatch(FudgeMsg msg, FudgeContext fudgeContext) { FudgeMsgEnvelope result = CycleMessage(fudgeContext, msg); Assert.NotNull(result); Assert.NotNull(result.Message); FudgeMsg resultMsg = result.Message; FudgeUtils.AssertAllFieldsMatch(msg, resultMsg); }
/// <summary> /// Constructs a new instance. /// </summary> /// <param name="context">Context for the writer.</param> /// <param name="writer"><see cref="TextWriter"/> to receive the output.</param> public FudgeJSONStreamWriter(FudgeContext context, TextWriter writer) { if (context == null) throw new ArgumentNullException("context"); if (writer == null) throw new ArgumentNullException("writer"); this.context = context; this.writer = writer; }
public void GettingTypeData() { var context = new FudgeContext(); var cache = new TypeDataCache(context); TypeData data = cache.GetTypeData(this.GetType(), FudgeFieldNameConvention.Identity); Assert.NotNull(data); TypeData data2 = cache.GetTypeData(this.GetType(), FudgeFieldNameConvention.Identity); Assert.Same(data, data2); }
public void PicksUpPropertiesFromContext() { var context = new FudgeContext(); var map1 = new SerializationTypeMap(context); Assert.True(map1.AllowTypeDiscovery); context.SetProperty(ContextProperties.AllowTypeDiscoveryProperty, false); var map2 = new SerializationTypeMap(context); Assert.False(map2.AllowTypeDiscovery); }
/// <summary> /// Constructs a new instance. /// </summary> /// <param name="context">Context for the writer.</param> /// <param name="writer"><see cref="TextWriter"/> to receive the output.</param> public FudgeJSONStreamWriter(FudgeContext context, TextWriter writer) { if (context == null) throw new ArgumentNullException("context"); if (writer == null) throw new ArgumentNullException("writer"); this.context = context; this.writer = writer; this.settings = (JSONSettings)context.GetProperty(JSONSettings.JSONSettingsProperty) ?? new JSONSettings(); }
public static FudgeMsg CreateMessageWithSubMsgs(FudgeContext context) { FudgeMsg inputMsg = context.NewMessage( new Field("sub1", new Field("bibble", "fibble"), new Field(827, "Blibble")), new Field("sub2", new Field("bibble9", 9837438), new Field(828, 82.77f))); return inputMsg; }
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)); }
/// <summary> /// Constructs a <see cref="FudgeJSONStreamReader"/> on a given <see cref="TextReader"/>. /// </summary> /// <param name="context">Context to control behaviours.</param> /// <param name="settings">Settings to override any in the <see cref="FudgeContext"/>.</param> /// <param name="reader"><see cref="TextReader"/> providing the data.</param> public FudgeJSONStreamReader(FudgeContext context, JSONSettings settings, TextReader reader) { if (context == null) throw new ArgumentNullException("context"); if (reader == null) throw new ArgumentNullException("reader"); if (settings == null) throw new ArgumentNullException("settings"); this.context = context; this.reader = reader; this.settings = settings; }
public void AllowTypeDiscoveryBehaviour() { var context = new FudgeContext(); var map = new SerializationTypeMap(context); map.AllowTypeDiscovery = false; Assert.Null(map.GetSurrogate(typeof(Reflect.Tick))); Assert.Equal(-1, map.GetTypeId(typeof(Reflect.Tick))); map.AllowTypeDiscovery = true; Assert.NotNull(map.GetSurrogate(typeof(Reflect.Tick))); Assert.NotEqual(-1, map.GetTypeId(typeof(Reflect.Person))); }
/// <summary> /// Constructs a new instance. /// </summary> /// <param name="context">Context for the writer.</param> /// <param name="settings">Settings for the writer (rather than getting from the context).</param> /// <param name="writer"><see cref="TextWriter"/> to receive the output.</param> public FudgeJSONStreamWriter(FudgeContext context, JSONSettings settings, TextWriter writer) { if (context == null) throw new ArgumentNullException("context"); if (writer == null) throw new ArgumentNullException("writer"); if (settings == null) throw new ArgumentNullException("settings"); this.context = context; this.writer = writer; this.settings = settings; }
public void AllMessagesSameContext() { FudgeContext fudgeContext = new FudgeContext(); FudgeMsg msg = null; msg = StandardFudgeMessages.CreateMessageAllNames(fudgeContext); CheckResultsMatch(msg, fudgeContext); msg = StandardFudgeMessages.CreateMessageAllOrdinals(fudgeContext); CheckResultsMatch(msg, fudgeContext); msg = StandardFudgeMessages.CreateMessageAllByteArrayLengths(fudgeContext); CheckResultsMatch(msg, fudgeContext); msg = StandardFudgeMessages.CreateMessageWithSubMsgs(fudgeContext); CheckResultsMatch(msg, fudgeContext); }
/// <summary> /// Constructs a new <see cref="DotNetSerializationSurrogateSurrogate"/>. /// </summary> /// <param name="context"><see cref="FudgeContext"/> to use.</param> /// <param name="typeData"><see cref="TypeData"/> for the type for this surrogate.</param> /// <param name="surrogate">Surrogate that maps the object to or from a <see cref="SerializationInfo"/>.</param> /// <param name="selector">Selector that produced the surrogate.</param> public DotNetSerializationSurrogateSurrogate(FudgeContext context, TypeData typeData, ISerializationSurrogate surrogate, ISurrogateSelector selector) { if (context == null) throw new ArgumentNullException("context"); if (typeData == null) throw new ArgumentNullException("typeData"); if (surrogate == null) throw new ArgumentNullException("surrogate"); // Don't care if selector is null this.helper = new SerializationInfoMixin(context, typeData.Type, new BeforeAfterSerializationMixin(context, typeData)); this.surrogate = surrogate; this.selector = selector; }
/// <summary> /// Constructs a new <c>FudgeXmlStreamWriter</c>, outputting to a given <see cref="XmlWriter"/>. /// </summary> /// <param name="context">Context to control behaviours.</param> /// <param name="writer"><see cref="XmlWriter"/> to use to output XML.</param> /// <param name="outerElementName">The name of the XML element used for outermost messages.</param> public FudgeXmlStreamWriter(FudgeContext context, XmlWriter writer, string outerElementName) { if (context == null) throw new ArgumentNullException("context"); if (writer == null) throw new ArgumentNullException("writer"); if (outerElementName == null) throw new ArgumentNullException("outerElementName"); this.context = context; this.writer = writer; this.outerElementName = outerElementName; this.AutoFlushOnMessageEnd = (bool)context.GetProperty(AutoFlushOnMessageEndProperty, true); }
/// <summary> /// Constructs a new <see cref="DotNetSerializableSurrogate"/>. /// </summary> /// <param name="context"><see cref="FudgeContext"/> to use.</param> /// <param name="typeData"><see cref="TypeData"/> for the type for this surrogate.</param> public DotNetSerializableSurrogate(FudgeContext context, TypeData typeData) { if (context == null) throw new ArgumentNullException("context"); if (typeData == null) throw new ArgumentNullException("typeData"); if (!CanHandle(typeData)) throw new ArgumentOutOfRangeException("typeData", "DotNetSerializableSurrogate cannot handle " + typeData.Type.FullName); this.context = context; this.type = typeData.Type; this.constructor = FindConstructor(typeData); helper = new SerializationInfoMixin(context, typeData.Type, new BeforeAfterSerializationMixin(context, typeData)); }
/// <summary> /// Constructs a new <see cref="PropertyBasedSerializationSurrogate"/>. /// </summary> /// <param name="context"><see cref="FudgeContext"/> to use.</param> /// <param name="typeData"><see cref="TypeData"/> for the type for this surrogate.</param> public PropertyBasedSerializationSurrogate(FudgeContext context, TypeData typeData) { if (context == null) throw new ArgumentNullException("context"); if (typeData == null) throw new ArgumentNullException("typeData"); if (!CanHandle(typeData)) throw new ArgumentOutOfRangeException("typeData", "PropertyBasedSerializationSurrogate cannot handle " + typeData.Type.FullName); Debug.Assert(typeData.DefaultConstructor != null); // Should have been caught in CanHandle() var constructor = typeData.DefaultConstructor; this.memberSerializer = new MemberSerializerMixin(context, typeData, typeData.Properties, new BeforeAfterSerializationMixin(context, typeData), () => constructor.Invoke(null)); }
public void GeneralTest() { var context = new FudgeContext(); var msg = StandardFudgeMessages.CreateMessageWithSubMsgs(context); var reader = new FudgeMsgStreamReader(context, msg); var writer = new FudgeMsgStreamWriter(); var pipe = new FudgeStreamPipe(reader, writer); pipe.Process(); var newMsg = writer.DequeueMessage(); FudgeUtils.AssertAllFieldsMatch(msg, newMsg); }
/// <summary> /// Constructs a new <see cref="ImmutableSurrogate"/>. /// </summary> /// <param name="context"><see cref="FudgeContext"/> to use.</param> /// <param name="typeData"><see cref="TypeData"/> for the type for this surrogate.</param> public ImmutableSurrogate(FudgeContext context, TypeData typeData) { if (context == null) throw new ArgumentNullException("context"); if (typeData == null) throw new ArgumentNullException("typeData"); if (!CanHandle(typeData)) throw new ArgumentOutOfRangeException("typeData", "ImmutableSurrogate cannot handle " + typeData.Type.FullName); this.context = context; this.type = typeData.Type; this.constructor = FindConstructor(typeData.Constructors, typeData.Properties, out constructorParams); Debug.Assert(constructor != null); // Else how did it pass CanHandle? this.helper = new PropertyBasedSerializationSurrogate.PropertySerializerMixin(context, typeData, typeData.Properties, new DotNetSerializableSurrogate.BeforeAfterMethodMixin(context, typeData)); }
internal static FudgeMsg CreateMessageAllByteArrayLengths(FudgeContext context) { FudgeMsg msg = context.NewMessage(); msg.Add("byte[4]", new byte[4]); msg.Add("byte[8]", new byte[8]); msg.Add("byte[16]", new byte[16]); msg.Add("byte[20]", new byte[20]); msg.Add("byte[32]", new byte[32]); msg.Add("byte[64]", new byte[64]); msg.Add("byte[128]", new byte[128]); msg.Add("byte[256]", new byte[256]); msg.Add("byte[512]", new byte[512]); msg.Add("byte[28]", new byte[28]); return msg; }