public void FindProperty_FindsIgnoredProperties() { SerializationContext context = new SerializationContext(); TypeData handler = context.GetTypeHandler(typeof(IgnoredFieldClass)); IPropertyData fieldHandler = handler.FindProperty("IVal"); Assert.IsNotNull(fieldHandler, "Ignored property not found"); }
public void IsCollection_ReturnsDefaultValueForJsonExCollection_WhenItemTypeOnlySpecified() { SerializationContext context = new SerializationContext(); TypeData CollectionTypeHandler = context.TypeHandlerFactory[typeof(StronglyTypedCollection2)]; Assert.IsTrue(CollectionTypeHandler.IsCollection(), "Strongly Typed collection is a collection"); Assert.IsInstanceOfType(typeof(CollectionHandlerWrapper), CollectionTypeHandler.CollectionHandler, "Wrong collection handler"); Assert.AreSame(typeof(string), CollectionTypeHandler.CollectionHandler.GetItemType(typeof(StronglyTypedCollection)), "Wrong collection item type"); }
/// <summary> /// Constructs a serializer to (de)serialize the given type using the /// specified configuration section. /// </summary> /// <param name="t">the type to serialize/deserialize</param> public Serializer(Type type, string configSection) { if (type == null) throw new ArgumentNullException("type"); _serializedType = type; _context = new SerializationContext(); _context.SerializerInstance = this; XmlConfigurator.Configure(_context, configSection); }
/// <summary> /// Constructs a serializer with an existing context to (de)serialize the given type /// </summary> /// <param name="t">the type to serialize/deserialize</param> /// <param name="context"></param> public Serializer(Type type, SerializationContext context) { if (type == null) throw new ArgumentNullException("type"); if (context == null) throw new ArgumentNullException("context"); _serializedType = type; _context = context; _context.SerializerInstance = this; }
public override object ConvertTo(object item, Type sourceType, SerializationContext serializationContext) { string data = (string)item; if (data.IndexOf(',') != -1) { string[] splitData = data.Split(','); return new MyImmutablePoint(int.Parse(splitData[0]), int.Parse(splitData[1])); } else { return new MyImmutablePoint(0, 0); } }
public void IgnoreProperty_DoesNotDeleteField() { SerializationContext context = new SerializationContext(); TypeData handler = context.GetTypeHandler(typeof(SimpleObject)); foreach(IPropertyData prop in handler.AllProperties) ; // force properties to load handler.IgnoreProperty("IntValue"); bool found = false; foreach (IPropertyData prop in handler.AllProperties) if (prop.Name == "IntValue") found = true; Assert.IsTrue(found, "Ignored property deleted"); }
private XmlConfigurator(XmlReader reader, SerializationContext context, string sectionName) { this.reader = reader; this.context = context; this.sectionName = sectionName; handlers["IsCompact"] = delegate() { context.IsCompact = reader.ReadElementContentAsBoolean(); }; handlers["OutputTypeComment"] = delegate() { context.OutputTypeComment = reader.ReadElementContentAsBoolean(); }; handlers["OutputTypeInformation"] = delegate() { context.OutputTypeInformation = reader.ReadElementContentAsBoolean(); }; handlers["ReferenceWritingType"] = new MapHandler(HandleReferenceWritingType); handlers["TypeBindings"] = new MapHandler(HandleTypeBindings); handlers["TypeConverters"] = new MapHandler(HandleTypeConverters); handlers["CollectionHandlers"] = new MapHandler(HandleCollectionHandlers); handlers["IgnoreProperties"] = new MapHandler(HandleIgnoreProperties); }
public void TestHasConverter() { SerializationContext ctx = new SerializationContext(); bool f = ctx.TypeHandlerFactory[typeof(Guid)].HasConverter; Assert.IsTrue(f, "Guid should have implicit TypeConverter through System.ComponentModel framework"); f = ctx.TypeHandlerFactory[typeof(SimpleObject)].HasConverter; Assert.IsFalse(f, "SimpleObject should not have converter"); f = ctx.TypeHandlerFactory[typeof(SimpleObject)].FindProperty("ByteValue").HasConverter; Assert.IsFalse(f, "SimpleObject.ByteValue property does not have a converter"); // test primitives f = ctx.TypeHandlerFactory[typeof(int)].HasConverter; Assert.IsFalse(f, "No converters for primitive types"); f = ctx.TypeHandlerFactory[typeof(string)].HasConverter; Assert.IsFalse(f, "No converters for string"); }
public static void Configure(SerializationContext context, XmlReader reader, string sectionName) { new XmlConfigurator(reader, context, sectionName).Configure(); }
public override object ConvertFrom(object item, SerializationContext serializationContext) { MyImmutablePoint pt = (MyImmutablePoint) item; return pt.X + "," + pt.Y; }
public void AttributeProcessors_CustomProcessor_XmlIgnore() { SerializationContext context = new SerializationContext(); context.TypeHandlerFactory.AttributeProcessors.Add(new XmlIgnoreAttributeProcessor()); TypeData handler = context.GetTypeHandler(typeof(XmlIgnoreMock)); Assert.IsTrue(handler.FindProperty("Salary").Ignored, "XmlIgnore attribute not ignored"); }
public void IgnoreProperty_WhenFieldDoesntExist_ThrowsError() { SerializationContext context = new SerializationContext(); TypeData handler = context.GetTypeHandler(typeof(SimpleObject)); handler.IgnoreProperty("Foo"); }
public void TypeDataNamingStrategy_AppliedToProperties() { SerializationContext config = new SerializationContext(); config.TypeHandlerFactory.SetPropertyNamingStrategy(new UnderscoreNamingStrategy()); TypeData td = config.TypeHandlerFactory[typeof(SimpleObject)]; IPropertyData pd = td.FindPropertyByName("ByteValue"); Assert.AreEqual("Byte_Value", pd.Alias, "ByteValue Alias"); IPropertyData bvByAlias = td.FindPropertyByAlias("Byte_Value"); Assert.AreSame(pd, bvByAlias, "ByteValue By Alias"); }
public void IsEmpty_OnNonEmptyClass_ReturnsFalse() { SerializationContext context = new SerializationContext(); TypeData SimpleObjectHandler = new TypeData(typeof(SimpleObject), context); Assert.IsFalse(SimpleObjectHandler.IsEmpty, "IsEmpty should return false on class with properties/fields"); }
public void IsEmpty_OnClassWithOnlyIgnoredFields_ReturnsTrue() { SerializationContext context = new SerializationContext(); TypeData IgnoredClassHandler = new TypeData(typeof(IgnoredFieldClass), context); Assert.IsTrue(IgnoredClassHandler.IsEmpty, "IsEmpty should return true on class with all properties/fields ignored"); }
public void IsEmpty_OnEmptyClass_ReturnsTrue() { SerializationContext context = new SerializationContext(); TypeData EmptyClassHandler = new TypeData(typeof(EmptyClass), context); Assert.IsTrue(EmptyClassHandler.IsEmpty, "IsEmpty should return true on class with no properties/fields"); }
public void Ignored_WhenNoSetter_DefaultsToTrue() { SerializationContext context = new SerializationContext(); TypeData handler = context.TypeHandlerFactory[typeof(NonWritableProperty)]; Assert.IsTrue(handler.FindProperty("NoSetter").Ignored); }
public static Serializer GetSerializer(Type type, SerializationContext context) { return new Serializer(type, context); }
public static void Configure(SerializationContext context, string configSection) { XmlConfigSection section = (XmlConfigSection)ConfigurationManager.GetSection(configSection); if (section == null && configSection != "JsonExSerializer") throw new ArgumentException("Unable to find config section " + configSection); if (section == null) return; string xml = section.RawXml; XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); Configure(context, XmlReader.Create(new StringReader(xml)), configSection); }
public void Setup() { serializer = new Serializer(typeof(object), "SimpleSettingsConfig"); context = serializer.Context; }