public void TestSetArrayName() { var container = new XmlMappingContainer(); container.SetType <User>("MapUser"); Assert.IsFalse(container.Get <User>().HasXmlArrayName); container.SetType <User>("MapUser").SetArrayName("Users"); Assert.IsTrue(container.Get <User>().HasXmlArrayName); Assert.AreEqual("Users", container.Get <User>().XmlArrayName); }
public void TestSetProperties() { var container = new XmlMappingContainer(); container.SetType <User>("MapUser") .SetProperty("UserName", "MapUserName"); Assert.IsTrue(container.Get <User>().Has("UserName")); var result = container.Get <User>().Properties["UserName"]; Assert.AreEqual("UserName", result.PropertyName); Assert.AreEqual("MapUserName", result.XmlPropertyName); }
public XmlArray ToXmlArray(Type type, IEnumerable enumerable, XmlMappingContainer mappings = null) { var singleItemType = this.assemblyInfoService.GetSingleItemType(type); if (singleItemType == null) { throw new JsonLibException("Require an enumerable"); } XmlTypeMapping mapping = null; if (mappings != null && mappings.Has(singleItemType)) { mapping = mappings.Get(singleItemType); } var nullableType = Nullable.GetUnderlyingType(singleItemType); if (nullableType != null) { var mainNodeName = mapping != null && mapping.HasXmlArrayName ? mapping.XmlArrayName : "ArrayOf" + nullableType.Name; return(this.DoToXmlArray(type, singleItemType, mainNodeName, enumerable, mappings)); } else { var mainNodeName = mapping != null && mapping.HasXmlArrayName ? mapping.XmlArrayName : "ArrayOf" + singleItemType.Name; return(this.DoToXmlArray(type, singleItemType, mainNodeName, enumerable, mappings)); } }
public object ToObject(Type objType, XmlObject xmlObject, XmlMappingContainer mappings = null) { var instance = this.assemblyInfoService.CreateInstance(objType); var properties = this.assemblyInfoService.GetProperties(instance); bool hasMappings = mappings != null; XmlTypeMapping mapping = null; if (hasMappings && mappings.Has(objType)) { mapping = mappings.Get(objType); } foreach (var xmlValue in xmlObject.Values) { var property = hasMappings ? this.FindProperty(properties, xmlValue.Key, mapping) : this.FindProperty(properties, xmlValue.Key); if (property != null) { var value = this.Resolve(property.PropertyType, xmlValue.Value, mappings); this.assemblyInfoService.SetValue(instance, property, value); } } return(instance); }
public XmlArray ToXmlArray_FromDictionary(Type type, IDictionary dictionary, XmlMappingContainer mappings = null) { var keyType = this.assemblyInfoService.GetDictionaryKeyType(type); var valueType = this.assemblyInfoService.GetDictionaryValueType(type); XmlTypeMapping mapping = null; if (mappings != null && mappings.Has(valueType)) { mapping = mappings.Get(valueType); } var nullableType = Nullable.GetUnderlyingType(valueType); if (nullableType != null) { var mainNodeName = mapping != null && mapping.HasXmlArrayName ? mapping.XmlArrayName : "ArrayOf" + nullableType.Name; return(this.DoToXmlArray_FromDictionary(type, keyType, nullableType, dictionary, mainNodeName, mappings)); } else { var mainNodeName = mapping != null && mapping.HasXmlArrayName ? mapping.XmlArrayName : "ArrayOf" + valueType.Name; return(this.DoToXmlArray_FromDictionary(type, keyType, valueType, dictionary, mainNodeName, mappings)); } }
public XmlObject ToXmlObject(Type type, object obj, string nodeName, XmlMappingContainer mappings = null) { XmlTypeMapping mapping = null; if (mappings != null && mappings.Has(type)) { mapping = mappings.Get(type); } return(this.DoToXmlObject(type, obj, nodeName, mapping, mappings)); }
public void TestGetType() { var container = new XmlMappingContainer(); container.SetType <User>("MapUser"); var result = container.Get <User>(); Assert.AreEqual("MapUser", result.XmlTypeName); Assert.IsFalse(result.HasXmlArrayName); }
public XmlObject ToXmlObject(Type type, object obj, XmlMappingContainer mappings = null) { XmlTypeMapping mapping = null; if (mappings != null && mappings.Has(type)) { mapping = mappings.Get(type); } var nodeName = mapping != null ? mapping.XmlTypeName : type.Name; return(this.DoToXmlObject(type, obj, nodeName, mapping, mappings)); }
public void TestDontFindProperty() { var service = this.GetService(); var properties = typeof(User).GetProperties(); var mappings = new XmlMappingContainer(); mappings.SetType <User>("MapUser"); var result = service.FindProperty(properties, "MapUserName", mappings.Get <User>()); Assert.AreEqual(null, result); }
public IXmlValue ToXmlValue(Type type, object value, XmlMappingContainer mappings = null) { XmlTypeMapping mapping = null; if (mappings != null && mappings.Has(type)) { mapping = mappings.Get(type); } bool hasMapping = mapping != null; if (this.assemblyInfoService.IsSystemType(type)) { if (type == typeof(string)) { var nodeName = hasMapping ? mapping.XmlTypeName : "String"; return(new XmlString(nodeName, this.GetStringValueOrNull(value))); } else if (this.assemblyInfoService.IsNumberType(type)) { var nodeName = hasMapping ? mapping.XmlTypeName : type.Name; return(new XmlNumber(nodeName, value)); } else if (type == typeof(bool)) { var nodeName = hasMapping ? mapping.XmlTypeName : "Boolean"; return(new XmlBool(nodeName, Convert.ToBoolean(value))); } else if (type == typeof(DateTime)) { var nodeName = hasMapping ? mapping.XmlTypeName : "DateTime"; return(new XmlString(nodeName, value.ToString())); } else if (type == typeof(Guid)) { var nodeName = hasMapping ? mapping.XmlTypeName : "Guid"; return(new XmlString(nodeName, value.ToString())); } else if (this.assemblyInfoService.IsNullable(type)) { var underlyingType = Nullable.GetUnderlyingType(type); if (underlyingType != null) { var nodeName = hasMapping ? mapping.XmlTypeName : underlyingType.Name; return(new XmlNullable(type, nodeName, value)); } } else if (typeof(IEnumerable).IsAssignableFrom(value.GetType())) { // array example string[] return(this.ToXmlArray(type, (IEnumerable)value, mappings)); } } else if (this.assemblyInfoService.IsDictionary(type)) { return(this.ToXmlArray_FromDictionary(type, (IDictionary)value, mappings)); } else if (this.assemblyInfoService.IsEnum(type)) { var nodeName = hasMapping ? mapping.XmlTypeName : type.Name; return(new XmlString(nodeName, value.ToString())); } else if (typeof(IEnumerable).IsAssignableFrom(value.GetType())) { return(this.ToXmlArray(type, (IEnumerable)value, mappings)); } else { return(this.ToXmlObject(type, value, mappings)); } throw new JsonLibException("Cannot resolve object"); }