void CastGegericTest() { var obj = new { OkProp = 1 }; var target = DataAccessResolver.Execute <object>(obj, new HashSet <string>() { $"{obj.GetType().Name}.OkProp" }); var serialized = JsonConvert.SerializeObject(target, Formatting.None); Assert.Equal("{\"okProp\":1}", serialized); }
void IgnoresSpecifiedPropertiesTest() { var obj = new { OkProp = 1, BadProp = 2, BadProp2 = 3 }; var target = DataAccessResolver.Execute(obj, new HashSet <string>() { $"{obj.GetType().Name}.OkProp" }); var serialized = JsonConvert.SerializeObject(target, Formatting.None); Assert.Equal("{\"okProp\":1}", serialized); }
void ContainsSubPropertiesTest() { var subObj = new { SubOne = 2.1, SubTwo = 2.2 }; var obj = new { One = 1, Two = subObj, Three = 3 }; const string expected = "{\"one\":1,\"two\":{\"subOne\":2.1,\"subTwo\":2.2},\"three\":3}"; var target = DataAccessResolver.Execute(obj, new HashSet <string>() { $"{obj.GetType().Name}.One", $"{obj.GetType().Name}.Two", $"{obj.GetType().Name}.Three" }); var serialized = JsonConvert.SerializeObject(target, Formatting.None); Assert.Equal(expected, serialized); }
void PostProcessingTest() { var obj = new { OkProp = 1, BadProp = 2, BadProp2 = 3 }; var target = DataAccessResolver.Execute(obj, new HashSet <string>() { $"{obj.GetType().Name}.OkProp" }, jo => { jo.Add("payload", "l33t"); }); var serialized = JsonConvert.SerializeObject(target, Formatting.None); Assert.Equal("{\"okProp\":1,\"payload\":\"l33t\"}", serialized); }
public static object Create <T>(T statUnit, StatUnitTypes type, ISet <string> propNames, bool?isReadonly = null) where T : class { var unitType = StatisticalUnitsTypeHelper.GetStatUnitMappingType(type); if (unitType != typeof(T)) { var currentType = statUnit.GetType(); propNames = new HashSet <string>( currentType.GetProperties() .Where(v => propNames.Contains(DataAccessAttributesHelper.GetName(unitType, v.Name))) .Select(v => DataAccessAttributesHelper.GetName(currentType, v.Name)) .ToList() ); } return(DataAccessResolver.Execute(statUnit, propNames, jo => { jo.Add("type", (int)type); jo.Add("readonly", isReadonly); })); }