public void TestMapping_WithLowerMultipleRegistrations()
        {
            var mapping = new JsonMappingContainer();

            mapping.SetType <User>().SetToLowerCaseStrategy();
            mapping.SetType <Product>().SetToLowerCaseStrategy();

            Assert.IsTrue(mapping.Get <User>().LowerCaseStrategy);
            Assert.IsTrue(mapping.Get <Product>().LowerCaseStrategy);
        }
        public void TestMapping_WithMultipleAndLowerDontInterfer()
        {
            var mapping = new JsonMappingContainer();

            mapping.SetType <User>().SetToLowerCaseStrategy(false);
            mapping.SetType <Product>().SetToLowerCaseStrategy();

            Assert.IsFalse(mapping.Get <User>().LowerCaseStrategy);
            Assert.IsTrue(mapping.Get <Product>().LowerCaseStrategy);
        }
        protected JsonObject ToJsonObject(object obj, JsonMappingContainer mappings = null)
        {
            var result = new JsonObject();

            var properties = this.assemblyInfoService.GetProperties(obj);

            var objType = obj.GetType();

            bool hasMappings = mappings != null;

            bool allLower = hasMappings && mappings.LowerStrategyForAllTypes;

            JsonTypeMapping mapping = null;

            if (mappings != null && mappings.Has(objType))
            {
                mapping = mappings.Get(objType);
            }

            foreach (var property in properties)
            {
                var jsonPropertyName = hasMappings ? this.GetJsonPropertyName(property.Name, allLower, mapping) : property.Name;
                var propertyValue    = property.GetValue(obj);
                var propertyType     = property.PropertyType;

                var jsonValue = this.ToJsonValue(propertyType, propertyValue, mappings);
                result.Add(jsonPropertyName, jsonValue);
            }

            return(result);
        }
        public void TestMapping()
        {
            var mapping = new JsonMappingContainer();

            // obj => to json ... lower strategy
            mapping.SetType <User>()
            .SetProperty("Id", "id")
            .SetProperty("UserName", "username")
            .SetProperty("Age", "age")
            .SetProperty("Email", "email");

            // json => property obj ... can not resolve
            // or if property name to lower == json name

            Assert.AreEqual(1, mapping.Count);
            Assert.IsTrue(mapping.Has <User>());

            var result = mapping.Get <User>();

            Assert.AreEqual(false, result.LowerCaseStrategy);
            Assert.AreEqual("Id", result.Properties["Id"].PropertyName);
            Assert.AreEqual("id", result.Properties["Id"].JsonName);
            Assert.AreEqual("UserName", result.Properties["UserName"].PropertyName);
            Assert.AreEqual("username", result.Properties["UserName"].JsonName);
            Assert.AreEqual("Age", result.Properties["Age"].PropertyName);
            Assert.AreEqual("age", result.Properties["Age"].JsonName);
            Assert.AreEqual("Email", result.Properties["Email"].PropertyName);
            Assert.AreEqual("email", result.Properties["Email"].JsonName);
        }
Esempio n. 5
0
        protected object ToObject(Type objType, JsonObject jsonObject, JsonMappingContainer mappings = null)
        {
            var instance   = this.assemblyInfoService.CreateInstance(objType);
            var properties = this.assemblyInfoService.GetProperties(instance);

            bool hasMappings = mappings != null;

            bool allLower = hasMappings && mappings.LowerStrategyForAllTypes;

            JsonTypeMapping mapping = null;

            if (hasMappings && mappings.Has(objType))
            {
                mapping = mappings.Get(objType);
            }

            foreach (var jsonValue in jsonObject.Values)
            {
                var property = hasMappings ? this.FindProperty(properties, jsonValue.Key, allLower, mapping)
                     : this.FindProperty(properties, jsonValue.Key);
                if (property != null)
                {
                    var value = this.Resolve(property.PropertyType, jsonValue.Value, mappings);
                    this.assemblyInfoService.SetValue(instance, property, value);
                }
            }

            return(instance);
        }
        public void TestMapping_WithLowerToFalse()
        {
            var mapping = new JsonMappingContainer();

            mapping.SetType <User>().SetToLowerCaseStrategy(false);

            Assert.IsFalse(mapping.Get <User>().LowerCaseStrategy);
        }
        public void TestMapping_WithLower()
        {
            var mapping = new JsonMappingContainer();

            mapping.SetType <User>().SetToLowerCaseStrategy();

            Assert.AreEqual(1, mapping.Count);
            Assert.IsTrue(mapping.Has <User>());

            var result = mapping.Get <User>();

            Assert.AreEqual(true, result.LowerCaseStrategy);
        }