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); }
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>()); }
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 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); }
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 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 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 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]); }
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); }
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); }
public void TestObject_WithLowerStrategy() { var service = this.GetService(); var mappings = new JsonMappingContainer(); mappings.SetType <User>().SetToLowerCaseStrategy(); 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); }
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); }
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); }