public void TestArray_OfObjectsWithMapping() { var service = this.GetService(); var g = new Guid("344ac1a2-9613-44d7-b64c-8d45b4585176"); var t = new DateTime(1990, 12, 12); var g2 = new Guid("344ac1a2-9613-44d7-b64c-8d45b4585178"); var t2 = new DateTime(1990, 10, 12); var json = "[{\"map_myguid\":\"344ac1a2-9613-44d7-b64c-8d45b4585176\",\"map_myint\":1,\"map_mydouble\":1.5,\"map_mystring\":\"my \\\"escape\\\" value\",\"map_mybool\":true,\"map_mynullable\":null,\"map_myenum\":1,\"map_mydate\":\"12/12/1990 00:00:00\",\"map_myobj\":{\"MyInnerString\":\"my \\\"inner\\\" value 1\"},\"map_mylist\":[\"a1\",\"b1\"],\"map_myarray\":[\"y1\",\"z1\"]},{\"map_myguid\":\"344ac1a2-9613-44d7-b64c-8d45b4585178\",\"map_myint\":2,\"map_mydouble\":2.5,\"map_mystring\":\"my \\\"escape\\\"value 2\",\"map_mybool\":false,\"map_mynullable\":null,\"map_myenum\":0,\"map_mydate\":\"12/10/1990 00:00:00\",\"map_myobj\":{\"MyInnerString\":\"my \\\"inner\\\" value 2\"},\"map_mylist\":[\"a2\",\"b2\"],\"map_myarray\":[\"y2\",\"z2\"]}]"; var mappings = new JsonMappingContainer(); mappings.SetType <AssemblyItem>() .SetProperty("MyGuid", "map_myguid") .SetProperty("MyInt", "map_myint") .SetProperty("MyDouble", "map_mydouble") .SetProperty("MyString", "map_mystring") .SetProperty("MyBool", "map_mybool") .SetProperty("MyEnum", "map_myenum") .SetProperty("MyDate", "map_mydate") .SetProperty("MyNullable", "map_mynullable") .SetProperty("MyObj", "map_myobj") .SetProperty("MyList", "map_mylist") .SetProperty("MyArray", "map_myarray"); var results = service.ToObject <AssemblyItem[]>(json, mappings); var result = results[0]; Assert.AreEqual(g, result.MyGuid); Assert.AreEqual(1, result.MyInt); Assert.AreEqual(1.5, result.MyDouble); Assert.AreEqual(true, result.MyBool); Assert.AreEqual(AssemblyEnum.Other, result.MyEnum); Assert.AreEqual(t, result.MyDate); Assert.AreEqual("my \"inner\" value 1", result.MyObj.MyInnerString); Assert.AreEqual(2, result.MyList.Count); Assert.AreEqual("a1", result.MyList[0]); Assert.AreEqual("b1", result.MyList[1]); Assert.AreEqual(2, result.MyArray.Length); Assert.AreEqual("y1", result.MyArray[0]); Assert.AreEqual("z1", result.MyArray[1]); var result2 = results[1]; Assert.AreEqual(g2, result2.MyGuid); Assert.AreEqual(2, result2.MyInt); Assert.AreEqual(2.5, result2.MyDouble); Assert.AreEqual(false, result2.MyBool); Assert.AreEqual(AssemblyEnum.Default, result2.MyEnum); Assert.AreEqual(t2, result2.MyDate); Assert.AreEqual("my \"inner\" value 2", result2.MyObj.MyInnerString); Assert.AreEqual(2, result2.MyList.Count); Assert.AreEqual("a2", result2.MyList[0]); Assert.AreEqual("b2", result2.MyList[1]); Assert.AreEqual(2, result2.MyArray.Length); Assert.AreEqual("y2", result2.MyArray[0]); Assert.AreEqual("z2", result2.MyArray[1]); }
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() { 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); }
public void TestObject_WithMapping() { var service = this.GetService(); var user = new User { Id = 10, UserName = "******" }; var mappings = new JsonMappingContainer(); mappings.SetType <User>() .SetProperty("Id", "map_id") .SetProperty("UserName", "map_username") .SetProperty("Email", "map_email"); var result = service.ToJsonValue(user, mappings); Assert.AreEqual(JsonValueType.Object, result.ValueType); Assert.AreEqual(JsonValueType.Number, ((JsonObject)result).Values["map_id"].ValueType); Assert.AreEqual(10, ((JsonNumber)((JsonObject)result).Values["map_id"]).Value); Assert.AreEqual(JsonValueType.String, ((JsonObject)result).Values["map_username"].ValueType); Assert.AreEqual("Marie", ((JsonString)((JsonObject)result).Values["map_username"]).Value); Assert.AreEqual(JsonValueType.Nullable, ((JsonObject)result).Values["Age"].ValueType); Assert.AreEqual(null, ((JsonNullable)((JsonObject)result).Values["Age"]).Value); Assert.AreEqual(JsonValueType.String, ((JsonObject)result).Values["map_email"].ValueType); Assert.AreEqual(null, ((JsonString)((JsonObject)result).Values["map_email"]).Value); }
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 JsonMappingContainer(); mappings.SetType <User>() .SetProperty("Id", "map_id") .SetProperty("UserName", "map_username") .SetProperty("Email", "map_email"); var result = service.ToJson(array, mappings); Assert.AreEqual("[{\"map_id\":1,\"map_username\":\"Marie\",\"Age\":null,\"map_email\":null},{\"map_id\":2,\"map_username\":\"Pat\",\"Age\":20,\"map_email\":\"[email protected]\"}]", result); }
public void TestObject_WithLowerStrategy() { var service = this.GetService(); var user = new User { Id = 10, UserName = "******" }; var mappings = new JsonMappingContainer(); mappings.SetType <User>().SetToLowerCaseStrategy(); var result = service.ToJsonValue(user, mappings); Assert.AreEqual(JsonValueType.Object, result.ValueType); Assert.AreEqual(JsonValueType.Number, ((JsonObject)result).Values["id"].ValueType); Assert.AreEqual(10, ((JsonNumber)((JsonObject)result).Values["id"]).Value); Assert.AreEqual(JsonValueType.String, ((JsonObject)result).Values["username"].ValueType); Assert.AreEqual("Marie", ((JsonString)((JsonObject)result).Values["username"]).Value); Assert.AreEqual(JsonValueType.Nullable, ((JsonObject)result).Values["age"].ValueType); Assert.AreEqual(null, ((JsonNullable)((JsonObject)result).Values["age"]).Value); Assert.AreEqual(JsonValueType.String, ((JsonObject)result).Values["email"].ValueType); Assert.AreEqual(null, ((JsonString)((JsonObject)result).Values["email"]).Value); }
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 TestDictionaryIntObject_WithMapping() { var service = this.GetService(); var mappings = new JsonMappingContainer(); mappings.SetType <User>().SetProperty("Id", "MapId") .SetProperty("UserName", "MapUserName") .SetProperty("Email", "MapEmail"); var json = "{\"10\":{\"MapId\":1,\"MapUserName\":\"Marie\",\"Age\":null,\"MapEmail\":null},\"20\":{\"MapId\":2,\"MapUserName\":\"Pat\",\"Age\":20,\"MapEmail\":\"[email protected]\"}}"; var result = service.ToObject <Dictionary <int, User> >(json, mappings); Assert.IsNotNull(result); Assert.AreEqual(1, result[10].Id); Assert.AreEqual("Marie", result[10].UserName); Assert.AreEqual(null, result[10].Age); Assert.AreEqual(null, result[10].Email); Assert.AreEqual(2, result[20].Id); Assert.AreEqual("Pat", result[20].UserName); Assert.AreEqual(20, result[20].Age); Assert.AreEqual("*****@*****.**", result[20].Email); }
public void TestAllithLower_ToFalse() { var mapping = new JsonMappingContainer(); mapping.SetLowerStrategyForAllTypes(false); Assert.IsFalse(mapping.LowerStrategyForAllTypes); }
public void TestMapping_WithLowerToFalse() { var mapping = new JsonMappingContainer(); mapping.SetType <User>().SetToLowerCaseStrategy(false); Assert.IsFalse(mapping.Get <User>().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); }
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); }
protected object ToList(Type type, JsonArray jsonArray, JsonMappingContainer mappings = null) { var singleItemType = this.assemblyInfoService.GetSingleItemType(type); var result = this.assemblyInfoService.CreateList(singleItemType); foreach (var jsonValue in jsonArray.Values) { var value = this.Resolve(singleItemType, jsonValue, mappings); result.Add(value); } return(result); }
public void TestMappings() { var mapping = new JsonMappingContainer(); mapping.SetType <User>().SetToLowerCaseStrategy(); mapping.SetType <Product>().SetToLowerCaseStrategy(); Assert.AreEqual(2, mapping.Count); Assert.IsTrue(mapping.Has <User>()); Assert.IsTrue(mapping.Has <Product>()); }
protected JsonArray ToJsonArray(IEnumerable array, JsonMappingContainer mappings = null) { var singleItemType = this.assemblyInfoService.GetSingleItemType(array.GetType()); var result = new JsonArray(); foreach (var value in array) { var jsonValue = this.ToJsonValue(singleItemType, value, mappings); result.Add(jsonValue); } return(result); }
protected object ToEnumerable(Type type, JsonArray jsonArray, JsonMappingContainer mappings = null) { if (this.assemblyInfoService.IsArray(type)) { return(this.ToArray(type, jsonArray, mappings)); } else if (this.assemblyInfoService.IsGenericType(type)) { return(this.ToList(type, jsonArray, mappings)); } return(null); }
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); }
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 JsonMappingContainer(); mappings.SetType <User>() .SetProperty("Id", "map_id") .SetProperty("UserName", "map_username") .SetProperty("Email", "map_email"); var result = service.ToJsonValue(array, mappings); Assert.AreEqual(JsonValueType.Array, result.ValueType); Assert.AreEqual(JsonValueType.Object, ((JsonArray)result).Values[0].ValueType); Assert.AreEqual(JsonValueType.Number, ((JsonObject)((JsonArray)result).Values[0]).Values["map_id"].ValueType); Assert.AreEqual(1, ((JsonNumber)((JsonObject)((JsonArray)result).Values[0]).Values["map_id"]).Value); Assert.AreEqual(JsonValueType.String, ((JsonObject)((JsonArray)result).Values[0]).Values["map_username"].ValueType); Assert.AreEqual("Marie", ((JsonString)((JsonObject)((JsonArray)result).Values[0]).Values["map_username"]).Value); Assert.AreEqual(JsonValueType.Nullable, ((JsonObject)((JsonArray)result).Values[0]).Values["Age"].ValueType); Assert.AreEqual(null, ((JsonNullable)((JsonObject)((JsonArray)result).Values[0]).Values["Age"]).Value); Assert.AreEqual(JsonValueType.String, ((JsonObject)((JsonArray)result).Values[0]).Values["map_email"].ValueType); Assert.AreEqual(null, ((JsonString)((JsonObject)((JsonArray)result).Values[0]).Values["map_email"]).Value); Assert.AreEqual(JsonValueType.Number, ((JsonObject)((JsonArray)result).Values[1]).Values["map_id"].ValueType); Assert.AreEqual(2, ((JsonNumber)((JsonObject)((JsonArray)result).Values[1]).Values["map_id"]).Value); Assert.AreEqual(JsonValueType.String, ((JsonObject)((JsonArray)result).Values[1]).Values["map_username"].ValueType); Assert.AreEqual("Pat", ((JsonString)((JsonObject)((JsonArray)result).Values[1]).Values["map_username"]).Value); Assert.AreEqual(JsonValueType.Nullable, ((JsonObject)((JsonArray)result).Values[1]).Values["Age"].ValueType); Assert.AreEqual(20, ((JsonNullable)((JsonObject)((JsonArray)result).Values[1]).Values["Age"]).Value); Assert.AreEqual(JsonValueType.String, ((JsonObject)((JsonArray)result).Values[1]).Values["map_email"].ValueType); Assert.AreEqual("*****@*****.**", ((JsonString)((JsonObject)((JsonArray)result).Values[1]).Values["map_email"]).Value); }
protected object ToArray(Type type, JsonArray jsonArray, JsonMappingContainer mappings = null) { var singleItemType = this.assemblyInfoService.GetSingleItemType(type); var result = this.assemblyInfoService.CreateArray(singleItemType, jsonArray.Values.Count); int index = 0; foreach (var jsonValue in jsonArray.Values) { var value = this.Resolve(singleItemType, jsonValue, mappings); result.SetValue(value, index); index++; } return(result); }
public T Parse <T>(string json, JsonMappingContainer mappings = null) { if (this.jsonCacheService.Has <T>(json)) { return((T)this.jsonCacheService.GetResult <T>(json)); } else { var result = this.jsonToObject.ToObject <T>(json, mappings); if (this.CacheIsActive) { this.jsonCacheService.Set <T>(json, result); } return(result); } }
public void TestObject_WithAllLower() { var service = this.GetService(); var mappings = new JsonMappingContainer().SetLowerStrategyForAllTypes(); var json = "{\"id\":1,\"username\":\"Marie\",\"age\":null,\"email\":null}"; var result = service.ToObject <User>(json, mappings); Assert.IsNotNull(result); Assert.AreEqual(1, result.Id); Assert.AreEqual("Marie", result.UserName); Assert.AreEqual(null, result.Age); Assert.AreEqual(null, result.Email); }
protected object Resolve(Type type, IJsonValue jsonValue, JsonMappingContainer mappings = null) { var jsonNillable = jsonValue as IJsonNillable; if (jsonNillable != null && jsonNillable.IsNil) { return(null); } else { if (jsonValue.ValueType == JsonValueType.Object) { if (this.assemblyInfoService.IsDictionary(type)) { return(this.ToDictionary(type, (JsonObject)jsonValue, mappings)); } else { return(this.ToObject(type, (JsonObject)jsonValue, mappings)); } } else if (jsonValue.ValueType == JsonValueType.Array) { return(this.ToEnumerable(type, (JsonArray)jsonValue, mappings)); } else if (jsonValue.ValueType == JsonValueType.String) { return(this.ToValue(type, (JsonString)jsonValue)); } else if (jsonValue.ValueType == JsonValueType.Number) { return(this.ToValue(type, (JsonNumber)jsonValue)); } else if (jsonValue.ValueType == JsonValueType.Bool) { return(this.ToValue(type, (JsonBool)jsonValue)); } else if (jsonValue.ValueType == JsonValueType.Nullable) { return(this.ToValue(type, (JsonNullable)jsonValue)); } throw new JsonLibException("Cannot resolve object for json"); } }
public void TestObject_WithMapping() { var service = this.GetService(); var mappings = new JsonMappingContainer(); mappings.SetType <User>().SetProperty("Id", "map_id").SetProperty("UserName", "map_username"); var json = "{\"map_id\":1,\"map_username\":\"Marie\",\"age\":null,\"email\":null}"; var result = service.ToObject <User>(json, mappings); Assert.IsNotNull(result); Assert.AreEqual(1, result.Id); Assert.AreEqual("Marie", result.UserName); Assert.AreEqual(null, result.Age); Assert.AreEqual(null, result.Email); }
public void TestObject_WithLowerStrategy() { var service = this.GetService(); var user = new User { Id = 10, UserName = "******" }; var mappings = new JsonMappingContainer(); mappings.SetType <User>().SetToLowerCaseStrategy(); var result = service.ToJson(user, mappings); Assert.AreEqual("{\"id\":10,\"username\":\"Marie\",\"age\":null,\"email\":null}", result); }
public void TestObject_WithMapping() { var service = this.GetService(); var user = new User { Id = 10, UserName = "******" }; var mappings = new JsonMappingContainer(); mappings.SetType <User>() .SetProperty("Id", "map_id") .SetProperty("UserName", "map_username") .SetProperty("Email", "map_email"); var result = service.ToJson(user, mappings); Assert.AreEqual("{\"map_id\":10,\"map_username\":\"Marie\",\"Age\":null,\"map_email\":null}", result); }
protected object ToDictionary(Type type, JsonObject jsonObject, JsonMappingContainer 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 jsonValue in jsonObject.Values) { var key = this.ResolveDictionaryKey(keyType, jsonValue.Key); var value = this.Resolve(valueType, jsonValue.Value, mappings); result.Add(key, value); } return(result); }
public void TestListOfObjects_WithLowerStrategy() { 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 JsonMappingContainer(); mappings.SetType <User>().SetToLowerCaseStrategy(); var result = service.ToJson(array, mappings); Assert.AreEqual("[{\"id\":1,\"username\":\"Marie\",\"age\":null,\"email\":null},{\"id\":2,\"username\":\"Pat\",\"age\":20,\"email\":\"[email protected]\"}]", result); }
public void TestDictionaryIntObject_WithAllLower() { var service = this.GetService(); var mappings = new JsonMappingContainer().SetLowerStrategyForAllTypes(); var json = "{\"10\":{\"id\":1,\"username\":\"Marie\",\"age\":null,\"email\":null},\"20\":{\"id\":2,\"username\":\"Pat\",\"age\":20,\"email\":\"[email protected]\"}}"; var result = service.ToObject <Dictionary <int, User> >(json, mappings); Assert.IsNotNull(result); Assert.AreEqual(1, result[10].Id); Assert.AreEqual("Marie", result[10].UserName); Assert.AreEqual(null, result[10].Age); Assert.AreEqual(null, result[10].Email); Assert.AreEqual(2, result[20].Id); Assert.AreEqual("Pat", result[20].UserName); Assert.AreEqual(20, result[20].Age); Assert.AreEqual("*****@*****.**", result[20].Email); }
public IJsonValue ToJsonValue <T>(T obj, JsonMappingContainer mappings = null) { return(this.ToJsonValue(typeof(T), obj, mappings)); }
public string ToJson <T>(T value, JsonMappingContainer mappings = null) { var jsonValue = this.objectToJsonValue.ToJsonValue <T>(value, mappings); return(this.jsonValueToJson.Resolve(jsonValue)); }