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); }
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 TestGetJsonPropertyName_WithMapping() { var service = this.GetService(); var mapping = new JsonTypeMapping(typeof(User)).SetProperty("UserName", "map_username"); var result = service.GetJsonPropertyName("UserName", false, mapping); Assert.AreEqual("map_username", result); }
public void TestGetJsonPropertyName_WithLowerMapping() { var service = this.GetService(); var mapping = new JsonTypeMapping(typeof(User)).SetToLowerCaseStrategy(); var result = service.GetJsonPropertyName("UserName", false, mapping); Assert.AreEqual("username", result); }
public void TestGetJsonPropertyName_WithAllLowerAndMapping_DontInterfer() { var service = this.GetService(); var mapping = new JsonTypeMapping(typeof(User)).SetProperty("UserName", "map_username"); var result = service.GetJsonPropertyName("UserName", true, mapping); Assert.AreEqual("username", result); }
public string GetJsonPropertyName(string propertyName, bool lowerCaseStrategyForAllTypes, JsonTypeMapping mapping) { if (lowerCaseStrategyForAllTypes) { return(propertyName.ToLower()); } else if (mapping != null) { if (mapping.LowerCaseStrategy) { return(propertyName.ToLower()); } else if (mapping.HasByPropertyName(propertyName)) { return(mapping.Properties[propertyName].JsonName); } } return(propertyName); }
public PropertyInfo FindProperty(PropertyInfo[] properties, string jsonName, bool lowerCaseStrategyForAllTypes, JsonTypeMapping mapping = null) { var propertyName = jsonName; var toLower = lowerCaseStrategyForAllTypes || (mapping != null && mapping.LowerCaseStrategy); if (toLower) { propertyName = propertyName.ToLower(); } else if (mapping != null && mapping.HasByJsonName(jsonName)) { propertyName = mapping.GetByJsonName(jsonName).PropertyName; } foreach (var property in properties) { var currentPropertyName = toLower ? property.Name.ToLower() : property.Name; if (currentPropertyName == propertyName) { return(property); } } return(null); }