public object ToDictionary(Type type, XmlArray xmlArray, XmlMappingContainer mappings = null)
        {
            var keyType   = this.assemblyInfoService.GetDictionaryKeyType(type);
            var valueType = this.assemblyInfoService.GetDictionaryValueType(type);

            var result = this.assemblyInfoService.CreateInstance(type) as IDictionary;

            if (result == null)
            {
                throw new JsonLibException("Cannot create dictionary");
            }

            foreach (var xmlValue in xmlArray.Values)
            {
                var xmlObject = xmlValue as XmlObject;
                if (xmlObject == null || xmlObject.Values.Count != 2)
                {
                    throw new JsonLibException("Cannot resolve dictionary from xml");
                }

                var xmlEntryKey = xmlObject.Values.ElementAt(0).Value;
                var key         = this.ResolveDictionaryKey(keyType, xmlEntryKey);

                var xmlEntryValue = xmlObject.Values.ElementAt(1).Value;
                var value         = this.Resolve(valueType, xmlEntryValue, mappings);

                result.Add(key, value);
            }

            return(result);
        }
        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);
        }
Esempio n. 3
0
        public void TestObjectWithMapping()
        {
            var service = this.GetService();

            var user = new User
            {
                Id       = 1,
                UserName = "******",
                Age      = 20,
                Email    = "*****@*****.**"
            };

            var mappings = new XmlMappingContainer();

            mappings.SetType <User>("MapUser")
            .SetProperty("Id", "MapId")
            .SetProperty("UserName", "MapUserName")
            .SetProperty("Email", "MapEmail");

            var xmlValue = new XmlObject("MapUser")
                           .AddNumber("MapId", user.Id)
                           .AddString("MapUserName", user.UserName)
                           .AddNullable("Age", user.Age)
                           .AddString("MapEmail", user.Email);

            var result = service.Resolve <User>(xmlValue, mappings);

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Id);
            Assert.AreEqual("Marie", result.UserName);
            Assert.AreEqual(20, result.Age);
            Assert.AreEqual("*****@*****.**", result.Email);
        }
Esempio n. 4
0
        public void TestObject_WithDictionaryAndMappingGuessOneObject()
        {
            var service  = this.GetService();
            var mappings = new XmlMappingContainer();

            mappings.SetType <MyItemWithDictionaryIntUser>("MyItemWithDictionaryIntUserEntity")
            .SetProperty("Users", "MyUsers");

            mappings.SetType <User>("MyUserEntity")
            .SetProperty("Id", "MyId")
            .SetProperty("UserName", "MyUserName")
            .SetProperty("Email", "MyEmail");

            var xml = "<?xml version=\"1.0\"?>\r<MyItemWithDictionaryIntUserEntity xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><MyUsers><User><Key>1</Key><Value><MyId>1</MyId><MyUserName>Marie</MyUserName><Age xsi:nil=\"true\" /><MyEmail xsi:nil=\"true\" /></Value></User></MyUsers></MyItemWithDictionaryIntUserEntity>";

            var result = service.ToObject <MyItemWithDictionaryIntUser>(xml, mappings);

            Assert.IsNotNull(result.Users);

            var dictionary = result.Users as Dictionary <int, User>;

            Assert.AreEqual(1, dictionary.Count);

            Assert.AreEqual(1, dictionary[1].Id);
            Assert.AreEqual("Marie", dictionary[1].UserName);
            Assert.AreEqual(null, dictionary[1].Age);
            Assert.AreEqual(null, dictionary[1].Email);
        }
        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 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 T ToObject <T>(string xml, XmlMappingContainer mappings = null)
        {
            var type     = typeof(T);
            var xmlValue = this.xmlToXmlValue.ToXmlValue(xml);

            return(this.xmlValueToObject.Resolve <T>(xmlValue, mappings));
        }
        public void TestSetType()
        {
            var container = new XmlMappingContainer();

            container.SetType <User>("MapUser");

            Assert.IsTrue(container.Has <User>());
        }
        public IXmlValue ToXmlValue(Type type, object value, string nodeName, XmlMappingContainer mappings = null)
        {
            if (this.assemblyInfoService.IsSystemType(type))
            {
                if (type == typeof(string))
                {
                    return(new XmlString(nodeName, this.GetStringValueOrNull(value)));
                }
                else if (this.assemblyInfoService.IsNumberType(type))
                {
                    return(new XmlNumber(nodeName, value));
                }
                else if (type == typeof(bool))
                {
                    return(new XmlBool(nodeName, Convert.ToBoolean(value)));
                }
                else if (type == typeof(DateTime))
                {
                    return(new XmlString(nodeName, value.ToString()));
                }
                else if (type == typeof(Guid))
                {
                    return(new XmlString(nodeName, value.ToString()));
                }
                else if (this.assemblyInfoService.IsNullable(type))
                {
                    var underlyingType = Nullable.GetUnderlyingType(type);
                    if (underlyingType != null)
                    {
                        return(new XmlNullable(type, nodeName, value));
                    }
                }
                else if (typeof(IEnumerable).IsAssignableFrom(value.GetType()))
                {
                    // array example string[]
                    return(this.ToXmlArray(type, (IEnumerable)value, nodeName, mappings));
                }
            }
            else if (this.assemblyInfoService.IsEnum(type))
            {
                return(new XmlString(nodeName, value.ToString()));
            }
            else if (this.assemblyInfoService.IsDictionary(type))
            {
                return(this.ToXmlArray_FromDictionary(type, (IDictionary)value, nodeName, mappings));
            }
            else if (typeof(IEnumerable).IsAssignableFrom(value.GetType()))
            {
                return(this.ToXmlArray(type, (IEnumerable)value, nodeName, mappings));
            }
            else
            {
                return(this.ToXmlObject(type, value, nodeName, mappings));
            }

            throw new JsonLibException("Cannot resolve object");
        }
        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 TestSetTypeRetrunsExistingType()
        {
            var container = new XmlMappingContainer();

            container.SetType <User>("MapUser").SetProperty("UserName", "MapuserName");

            var result = container.SetType <User>("MapUser");

            Assert.IsTrue(result.Has("UserName"));
        }
Esempio n. 12
0
        public void TestArrayWithMapping()
        {
            var service = this.GetService();

            var users = new List <User> {
                new User
                {
                    Id       = 1,
                    UserName = "******",
                },
                new User
                {
                    Id       = 2,
                    UserName = "******",
                    Age      = 20,
                    Email    = "*****@*****.**"
                }
            };

            var mappings = new XmlMappingContainer();

            mappings.SetType <User>("MapUser")
            .SetProperty("Id", "MapId")
            .SetProperty("UserName", "MapUserName")
            .SetProperty("Email", "MapEmail");

            var xmlValue = new XmlArray("MapUsers")
                           .Add(new XmlObject("MapUser")
                                .AddNumber("MapId", users[0].Id)
                                .AddString("MapUserName", users[0].UserName)
                                .AddNullable("Age", users[0].Age)
                                .AddString("MapEmail", users[0].Email))
                           .Add(new XmlObject("MapUser")
                                .AddNumber("MapId", users[1].Id)
                                .AddString("MapUserName", users[1].UserName)
                                .AddNullable("Age", users[1].Age)
                                .AddString("MapEmail", users[1].Email));

            var results = service.Resolve <List <User> >(xmlValue, mappings);

            var result = results[0];

            Assert.AreEqual(1, result.Id);
            Assert.AreEqual("Marie", result.UserName);
            Assert.AreEqual(null, result.Age);
            Assert.AreEqual(null, result.Email);

            var result2 = results[1];

            Assert.AreEqual(2, result2.Id);
            Assert.AreEqual("Pat", result2.UserName);
            Assert.AreEqual(20, result2.Age);
            Assert.AreEqual("*****@*****.**", result2.Email);
        }
        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);
        }
Esempio n. 14
0
        public void TestObject_WithMapping()
        {
            var service = this.GetService();

            //var user = new UserWithMapping
            //{
            //    Id = 1,
            //    UserName = "******",
            //    Age = 20,
            //    Email = "*****@*****.**",
            //    EnumValue = MyEnum.Other,
            //    Role = new UserRole { RoleName = "Admin" },
            //    Posts = new List<Post> { new Post { Title = "Post 1" } },
            //    Tips = new Tip[] { new Tip { TipName = "Tip 1" } }
            //};

            var mappings = new XmlMappingContainer();

            mappings.SetType <UserWithMapping>("MyUserEntity")
            .SetProperty("Id", "MyId")
            .SetProperty("UserName", "MyUserName")
            .SetProperty("Email", "MyEmail")
            .SetProperty("EnumValue", "MyEnumMap")
            .SetProperty("Role", "MyRole")
            .SetProperty("Posts", "MyPosts")
            .SetProperty("Tips", "MyTips");

            mappings.SetType <UserRole>("MyRoleEntity")
            .SetProperty("RoleName", "MyRoleName");

            mappings.SetType <Post>("MyPostEntity")
            .SetProperty("Title", "MyTitle");

            mappings.SetType <Tip>("MyTipEntity")
            .SetProperty("TipName", "MyTipName");

            // var x = JsonObjectSerializer.ToXml(user, mappings);

            var xml = "<?xml version=\"1.0\"?>\r<MyUserEntity><MyId>1</MyId><MyUserName>Marie</MyUserName><Age>20</Age><MyEmail>[email protected]</MyEmail><MyEnumMap>Other</MyEnumMap><MyRole><MyRoleName>Admin</MyRoleName></MyRole><MyPosts><MyPostEntity><MyTitle>Post 1</MyTitle></MyPostEntity></MyPosts><MyTips><MyTipEntity><MyTipName>Tip 1</MyTipName></MyTipEntity></MyTips></MyUserEntity>";

            var result = service.ToObject <UserWithMapping>(xml, mappings);

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Id);
            Assert.AreEqual("Marie", result.UserName);
            Assert.AreEqual(20, result.Age);
            Assert.AreEqual("*****@*****.**", result.Email);
            Assert.AreEqual(MyEnum.Other, result.EnumValue);
            Assert.AreEqual("Admin", result.Role.RoleName);
            Assert.AreEqual("Post 1", result.Posts[0].Title);
            Assert.AreEqual("Tip 1", result.Tips[0].TipName);
        }
        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 object ToList(Type type, XmlArray xmlArray, XmlMappingContainer mappings = null)
        {
            var singleItemType = type.GetGenericArguments()[0];
            var listType       = typeof(List <>).MakeGenericType(singleItemType);
            var result         = this.assemblyInfoService.CreateInstance(listType) as IList;

            foreach (var xmlValue in xmlArray.Values)
            {
                var value = this.Resolve(singleItemType, xmlValue, mappings);
                result.Add(value);
            }
            return(result);
        }
        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);
        }
Esempio n. 19
0
        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 object ToArray(Type type, XmlArray xmlArray, XmlMappingContainer mappings = null)
        {
            var singleItemType = type.GetTypeInfo().GetElementType();
            var result         = Array.CreateInstance(singleItemType, xmlArray.Values.Count);
            int index          = 0;

            foreach (var xmlValue in xmlArray.Values)
            {
                var value = this.Resolve(singleItemType, xmlValue, mappings);
                result.SetValue(value, index);
                index++;
            }
            return(result);
        }
        public object ToEnumerable(Type type, XmlArray xmlArray, XmlMappingContainer mappings = null)
        {
            if (this.assemblyInfoService.IsArray(type))
            {
                return(this.ToArray(type, xmlArray, mappings));
            }
            else if (this.assemblyInfoService.IsDictionary(type))
            {
                return(this.ToDictionary(type, xmlArray, mappings));
            }
            else if (this.assemblyInfoService.IsGenericType(type))
            {
                return(this.ToList(type, xmlArray, mappings));
            }

            throw new JsonLibException("Type is not enumerable " + type.Name);
        }
        public object Resolve(Type type, IXmlValue xmlValue, XmlMappingContainer mappings = null)
        {
            var xmlNillable = xmlValue as IXmlNillable;

            if (xmlNillable != null && xmlNillable.IsNil)
            {
                return(null);
            }
            else if (xmlValue.ValueType == XmlValueType.String)
            {
                return(this.ToValue(type, (XmlString)xmlValue));
            }
            else if (xmlValue.ValueType == XmlValueType.Number)
            {
                return(this.ToValue(type, (XmlNumber)xmlValue));
            }
            else if (xmlValue.ValueType == XmlValueType.Bool)
            {
                return(this.ToValue(type, (XmlBool)xmlValue));
            }
            else if (xmlValue.ValueType == XmlValueType.Nullable)
            {
                return(this.ToValue(type, (XmlNullable)xmlValue));
            }
            else if (xmlValue.ValueType == XmlValueType.Array)
            {
                return(this.ToEnumerable(type, (XmlArray)xmlValue, mappings));
            }
            else if (xmlValue.ValueType == XmlValueType.Object)
            {
                // guess is object with only one node value
                if (typeof(IEnumerable).IsAssignableFrom(type))
                {
                    var xmlArray = this.ConvertToArray((XmlObject)xmlValue);
                    return(this.ToEnumerable(type, xmlArray, mappings));
                }
                else
                {
                    return(this.ToObject(type, (XmlObject)xmlValue, mappings));
                }
            }

            throw new JsonLibException("Cannot resolve value");
        }
Esempio n. 23
0
        public void TestObject_WithDictionaryAndMapping()
        {
            var service = this.GetService();

            //var value = new MyItemWithDictionaryIntUser
            //{
            //    Users = new Dictionary<int, User>
            //    {
            //            { 1, new User { Id = 1, UserName = "******" } },
            //            { 2, new User { Id = 2, UserName = "******", Age = 20, Email = "*****@*****.**" } }
            //    }
            //};

            var mappings = new XmlMappingContainer();

            mappings.SetType <MyItemWithDictionaryIntUser>("MyItemWithDictionaryIntUserEntity")
            .SetProperty("Users", "MyUsers");

            mappings.SetType <User>("MyUserEntity")
            .SetProperty("Id", "MyId")
            .SetProperty("UserName", "MyUserName")
            .SetProperty("Email", "MyEmail");

            var xml = "<?xml version=\"1.0\"?>\r<MyItemWithDictionaryIntUserEntity xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><MyUsers><User><Key>1</Key><Value><MyId>1</MyId><MyUserName>Marie</MyUserName><Age xsi:nil=\"true\" /><MyEmail xsi:nil=\"true\" /></Value></User><User><Key>2</Key><Value><MyId>2</MyId><MyUserName>Pat</MyUserName><Age>20</Age><MyEmail>[email protected]</MyEmail></Value></User></MyUsers></MyItemWithDictionaryIntUserEntity>";

            var result = service.ToObject <MyItemWithDictionaryIntUser>(xml, mappings);

            Assert.IsNotNull(result.Users);

            var dictionary = result.Users as Dictionary <int, User>;

            Assert.AreEqual(2, dictionary.Count);

            Assert.AreEqual(1, dictionary[1].Id);
            Assert.AreEqual("Marie", dictionary[1].UserName);
            Assert.AreEqual(null, dictionary[1].Age);
            Assert.AreEqual(null, dictionary[1].Email);

            Assert.AreEqual(2, dictionary[2].Id);
            Assert.AreEqual("Pat", dictionary[2].UserName);
            Assert.AreEqual(20, dictionary[2].Age);
            Assert.AreEqual("*****@*****.**", dictionary[2].Email);
        }
Esempio n. 24
0
        public void TestArrayOfObjects_WithMappings()
        {
            var service = this.GetService();

            //var value = new List<User>
            //   {
            //        new User { Id = 1, UserName = "******" } ,
            //        new User { Id = 2, UserName = "******", Age = 20, Email = "*****@*****.**" }
            //   };

            // var x = JsonObjectSerializer.ToXml(value, mappings);

            var mappings = new XmlMappingContainer();

            mappings.SetType <User>("MyUserEntity")
            .SetProperty("Id", "MyId")
            .SetProperty("UserName", "MyUserName")
            .SetProperty("Email", "MyEmail");


            var xml = "<?xml version=\"1.0\"?>\r<ArrayOfUser xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><MyUserEntity><MyId>1</MyId><MyUserName>Marie</MyUserName><Age xsi:nil=\"true\" /><MyEmail xsi:nil=\"true\" /></MyUserEntity><MyUserEntity><MyId>2</MyId><MyUserName>Pat</MyUserName><Age>20</Age><MyEmail>[email protected]</MyEmail></MyUserEntity></ArrayOfUser>";

            var results = service.ToObject <User[]>(xml, mappings);

            var result = results[0];

            Assert.AreEqual(1, result.Id);
            Assert.AreEqual("Marie", result.UserName);
            Assert.AreEqual(null, result.Age);
            Assert.AreEqual(null, result.Email);

            var result2 = results[1];

            Assert.AreEqual(2, result2.Id);
            Assert.AreEqual("Pat", result2.UserName);
            Assert.AreEqual(20, result2.Age);
            Assert.AreEqual("*****@*****.**", result2.Email);
        }
Esempio n. 25
0
        public void TestArrayOfObjects_WithMappingsGuessObject()
        {
            var service = this.GetService();

            var mappings = new XmlMappingContainer();

            mappings.SetType <User>("MyUserEntity")
            .SetProperty("Id", "MyId")
            .SetProperty("UserName", "MyUserName")
            .SetProperty("Email", "MyEmail");


            var xml = "<?xml version=\"1.0\"?>\r<ArrayOfUser xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><MyUserEntity><MyId>1</MyId><MyUserName>Marie</MyUserName><Age xsi:nil=\"true\" /><MyEmail xsi:nil=\"true\" /></MyUserEntity></ArrayOfUser>";

            var results = service.ToObject <User[]>(xml, mappings);

            var result = results[0];

            Assert.AreEqual(1, result.Id);
            Assert.AreEqual("Marie", result.UserName);
            Assert.AreEqual(null, result.Age);
            Assert.AreEqual(null, result.Email);
        }
Esempio n. 26
0
        public void TestObject_WithMapping()
        {
            var service = this.GetService();

            var user = new User
            {
                Id       = 10,
                UserName = "******"
            };

            var mappings = new XmlMappingContainer();

            mappings.SetType <User>("user")
            .SetProperty("Id", "map_id")
            .SetProperty("UserName", "map_username")
            .SetProperty("Email", "map_email");

            var result = service.ToXmlValue(user, mappings);

            Assert.AreEqual(XmlValueType.Object, result.ValueType);

            Assert.AreEqual(XmlValueType.Number, ((XmlObject)result).Values["map_id"].ValueType);
            Assert.AreEqual("map_id", ((XmlObject)result).Values["map_id"].NodeName);
            Assert.AreEqual(10, ((XmlNumber)((XmlObject)result).Values["map_id"]).Value);

            Assert.AreEqual(XmlValueType.String, ((XmlObject)result).Values["map_username"].ValueType);
            Assert.AreEqual("map_username", ((XmlObject)result).Values["map_username"].NodeName);
            Assert.AreEqual("Marie", ((XmlString)((XmlObject)result).Values["map_username"]).Value);

            Assert.AreEqual(XmlValueType.Nullable, ((XmlObject)result).Values["Age"].ValueType);
            Assert.AreEqual("Age", ((XmlObject)result).Values["Age"].NodeName);
            Assert.AreEqual(null, ((XmlNullable)((XmlObject)result).Values["Age"]).Value);

            Assert.AreEqual(XmlValueType.String, ((XmlObject)result).Values["map_email"].ValueType);
            Assert.AreEqual("map_email", ((XmlObject)result).Values["map_email"].NodeName);
            Assert.AreEqual(null, ((XmlString)((XmlObject)result).Values["map_email"]).Value);
        }
Esempio n. 27
0
        public void TestListOfObjects_WithMappings()
        {
            var service = this.GetService();

            var array = new List <User>
            {
                new User {
                    Id = 1, UserName = "******"
                },
                new User {
                    Id = 2, UserName = "******", Age = 20, Email = "*****@*****.**"
                }
            };

            var mappings = new XmlMappingContainer();

            mappings.SetType <User>("user")
            .SetArrayName("MyUsers")
            .SetProperty("Id", "map_id")
            .SetProperty("UserName", "map_username")
            .SetProperty("Email", "map_email");

            var result = service.ToXmlValue(array, mappings) as XmlArray;

            Assert.AreEqual(XmlValueType.Array, result.ValueType);
            Assert.AreEqual("MyUsers", result.NodeName);

            Assert.AreEqual(XmlValueType.Object, ((XmlArray)result).Values[0].ValueType);

            Assert.AreEqual(XmlValueType.Number, ((XmlObject)result.Values[0]).Values["map_id"].ValueType);
            Assert.AreEqual("map_id", ((XmlObject)result.Values[0]).Values["map_id"].NodeName);
            Assert.AreEqual(1, ((XmlNumber)((XmlObject)((XmlArray)result).Values[0]).Values["map_id"]).Value);

            Assert.AreEqual(XmlValueType.String, ((XmlObject)result.Values[0]).Values["map_username"].ValueType);
            Assert.AreEqual("map_username", ((XmlObject)result.Values[0]).Values["map_username"].NodeName);
            Assert.AreEqual("Marie", ((XmlString)((XmlObject)result.Values[0]).Values["map_username"]).Value);

            Assert.AreEqual(XmlValueType.Nullable, ((XmlObject)result.Values[0]).Values["Age"].ValueType);
            Assert.AreEqual("Age", ((XmlObject)result.Values[0]).Values["Age"].NodeName);
            Assert.AreEqual(null, ((XmlNullable)((XmlObject)result.Values[0]).Values["Age"]).Value);

            Assert.AreEqual(XmlValueType.String, ((XmlObject)result.Values[0]).Values["map_email"].ValueType);
            Assert.AreEqual("map_email", ((XmlObject)result.Values[0]).Values["map_email"].NodeName);
            Assert.AreEqual(null, ((XmlString)((XmlObject)result.Values[0]).Values["map_email"]).Value);

            Assert.AreEqual(XmlValueType.Number, ((XmlObject)result.Values[1]).Values["map_id"].ValueType);
            Assert.AreEqual("map_id", ((XmlObject)result.Values[1]).Values["map_id"].NodeName);
            Assert.AreEqual(2, ((XmlNumber)((XmlObject)result.Values[1]).Values["map_id"]).Value);

            Assert.AreEqual(XmlValueType.String, ((XmlObject)result.Values[1]).Values["map_username"].ValueType);
            Assert.AreEqual("map_username", ((XmlObject)result.Values[1]).Values["map_username"].NodeName);
            Assert.AreEqual("Pat", ((XmlString)((XmlObject)result.Values[1]).Values["map_username"]).Value);

            Assert.AreEqual(XmlValueType.Nullable, ((XmlObject)result.Values[1]).Values["Age"].ValueType);
            Assert.AreEqual("Age", ((XmlObject)result.Values[1]).Values["Age"].NodeName);
            Assert.AreEqual(20, ((XmlNullable)((XmlObject)result.Values[1]).Values["Age"]).Value);

            Assert.AreEqual(XmlValueType.String, ((XmlObject)result.Values[1]).Values["map_email"].ValueType);
            Assert.AreEqual("map_email", ((XmlObject)result.Values[0]).Values["map_email"].NodeName);
            Assert.AreEqual("*****@*****.**", ((XmlString)((XmlObject)result.Values[1]).Values["map_email"]).Value);
        }
Esempio n. 28
0
        public string ToXmlAndBeautify <T>(T value, XmlMappingContainer mappings = null)
        {
            var xml = this.objectToXml.ToXml <T>(value, mappings);

            return(this.xmlBeautifier.Format(xml));
        }
Esempio n. 29
0
 public string ToXml <T>(T value, XmlMappingContainer mappings = null)
 {
     return(this.objectToXml.ToXml <T>(value, mappings));
 }
Esempio n. 30
0
 public T FromXml <T>(string xml, XmlMappingContainer mappings = null)
 {
     return(this.xmlToObject.ToObject <T>(xml, mappings));
 }