public static BusinessObject MapObject(Type businessObjectType, JObject jobject, HashSet <string> userPropertiesWhiteList, LenientParsing lenient = LenientParsing.Strict) { if (!businessObjectType.IsSubclassOf(typeof(BusinessObject))) { throw new ArgumentException("Mapping is only allowed for types derived from BO4E.BO.BusinessObject"); } else if (businessObjectType != null) { if (lenient == LenientParsing.Strict && userPropertiesWhiteList.Count == 0) { return((BusinessObject)jobject.ToObject(businessObjectType)); } else { var settings = lenient.GetJsonSerializerSettings(); return((BusinessObject)JsonConvert.DeserializeObject(jobject.ToString(), businessObjectType, settings)); } } else { return(null); } }
public void TestBoMapping() { string[] files = Directory.GetFiles($"BoMapperTests/", "*.json"); foreach (string file in files) { JObject json; using (StreamReader r = new StreamReader(file)) { string jsonString = r.ReadToEnd(); json = JsonConvert.DeserializeObject <JObject>(jsonString); } Assert.IsNotNull(json["objectName"], $"You have to specify the object name in test file {file}"); LenientParsing lenients = LenientParsing.Strict; // default if (json["lenientDateTime"] != null && (bool)json["lenientDateTime"]) { lenients |= LenientParsing.DateTime; } if (json["lenientEnumList"] != null && (bool)json["lenientEnumList"]) { lenients |= LenientParsing.EnumList; } if (json["lenientBo4eUri"] != null && (bool)json["lenientBo4eUri"]) { lenients |= LenientParsing.Bo4eUri; } if (json["lenientStringToInt"] != null && (bool)json["lenientStringToInt"]) { lenients |= LenientParsing.StringToInt; } BusinessObject bo; try { bo = JsonConvert.DeserializeObject <BusinessObject>(json["input"].ToString(), lenients.GetJsonSerializerSettings()); } catch (Exception e) when(e is ArgumentException || e is JsonSerializationException) { bo = BoMapper.MapObject(json["objectName"].ToString(), (JObject)json["input"], lenients); } string regularOutputString = JsonConvert.SerializeObject(bo, new StringEnumConverter()); if (bo.GetType() == typeof(Rechnung)) { continue; // todo: fix this! } /*if (json["input"]["boTyp"] != null) * { * //BusinessObject bo2 = BoMapper.MapObject((JObject)json["input"], lenients); * BusinessObject bo2 = JsonConvert.DeserializeObject<BusinessObject>(json["input"].ToString(), BoMapper.GetJsonSerializerSettings(lenients)); * //string regularOutputString2 = JsonConvert.SerializeObject(bo2, new StringEnumConverter()); * Assert.AreEqual(bo, bo2); * switch (json["input"]["boTyp"].ToString().ToLower()) * { * case "energiemenge": * //Assert.AreEqual((Energiemenge)bo, BoMapper.MapObject<Energiemenge>((JObject)json["input"], lenients)); * Assert.AreEqual((Energiemenge)bo, JsonConvert.DeserializeObject<Energiemenge>(json["input"].ToString(), BoMapper.GetJsonSerializerSettings(lenients))); * break; * case "messlokation": * //Assert.AreEqual((Messlokation)bo, BoMapper.MapObject<Messlokation>((JObject)json["input"], lenients)); * Assert.AreEqual((Messlokation)bo, JsonConvert.DeserializeObject<Messlokation>(json["input"].ToString(), BoMapper.GetJsonSerializerSettings(lenients))); * break; * // add more if you feel like * } * }*/ HashSet <string> whitelist; if (json["userPropWhiteList"] != null) { whitelist = new HashSet <string>(JArray.FromObject(json["userPropWhiteList"]).ToObject <List <string> >()); } else { whitelist = new HashSet <string>(); } if (lenients == LenientParsing.Strict) { foreach (LenientParsing lenient in Enum.GetValues(typeof(LenientParsing))) { // strict mappings must also work with lenient mapping BusinessObject boLenient; try { boLenient = JsonConvert.DeserializeObject <BusinessObject>(json["input"].ToString(), lenient.GetJsonSerializerSettings(whitelist)); } catch (ArgumentException) { boLenient = BoMapper.MapObject(json["objectName"].ToString(), (JObject)json["input"], whitelist, lenient); } catch (JsonSerializationException jse) { Assert.IsTrue(false, $"Unexpected {nameof(JsonSerializationException)} in file {file}: {jse.Message}"); throw jse; } //string dateLenietOutputString = JsonConvert.SerializeObject(boLenient, new StringEnumConverter()); //if (whitelist.Count ==0) { //Assert.AreEqual(regularOutputString, dateLenietOutputString); //} //else // { // Assert.AreEqual(regularOutputString, dateLenietOutputString); //} } } else { // non-strict test cases are designed such that they are not parseble in strict mode. // bool exceptionThrown; // try //{ // BusinessObject boStrict = BoMapper.MapObject(json["objectName"].ToString(), (JObject)json["input"], LenientParsing.Strict); // exceptionThrown = false; //} //catch (Exception) //{ // exceptionThrown = true; // } //Assert.IsTrue(exceptionThrown); } } }
public void TestVertragStringToInt() { // first test serialization of complete business object JObject json; using (StreamReader r = new StreamReader("BoMapperTests/Vertrag_lenient_String.json")) { string jsonString = r.ReadToEnd(); json = JsonConvert.DeserializeObject <JObject>(jsonString); } LenientParsing lenients = LenientParsing.StringToInt; Vertrag v = JsonConvert.DeserializeObject <Vertrag>(json["input"].ToString(), lenients.GetJsonSerializerSettings()); Assert.AreEqual(v.Vertragskonditionen.AnzahlAbschlaege, 12); }