public override ObservableList <ApplicationAPIModel> ParseDocument(string FileName, ObservableList <ApplicationAPIModel> AAMSList, bool avoidDuplicatesNodes = false) { ApplicationAPIModel AAM = new ApplicationAPIModel(); AAM.Name = Path.GetFileNameWithoutExtension(FileName); string JSOnText = System.IO.File.ReadAllText(FileName); //JObject jo = JObject.Parse(JSOnText); //IList<string> keys = jo.Properties().Select(p => p.Path).ToList(); if (avoidDuplicatesNodes) { JsonExtended fullJSOnObjectExtended = new JsonExtended(JSOnText); fullJSOnObjectExtended.RemoveDuplicatesNodes(); JSOnText = fullJSOnObjectExtended.JsonString; } object[] BodyandModelParameters = GenerateBodyANdModelParameters(JSOnText); AAM.RequestBody = (string)BodyandModelParameters[0]; AAM.AppModelParameters = (ObservableList <AppModelParameter>)BodyandModelParameters[1]; AAMSList.Add(AAM); return(AAMSList); }
public void AddAPIFromJSONAndAvoidDuplicateNodesTest() { //Arrange string JsonFilePath = TestResources.GetTestResourcesFile(@"JSON\Request JSON.TXT"); ObservableList <ApplicationAPIModel> AAMTempList = new ObservableList <ApplicationAPIModel>(); List <JsonExtended> jsonElements = new List <JsonExtended>(); List <JsonExtended> jsonElementsAvoidDuplicatesNodes = new List <JsonExtended>(); List <AppModelParameter> AppModelParameters = new List <AppModelParameter>(); List <AppModelParameter> AppModelParametersAvoidDuplicatesNodes = new List <AppModelParameter>(); //Act AAMTempList = new JSONTemplateParser().ParseDocument(JsonFilePath, AAMTempList, false); jsonElements = new JsonExtended(AAMTempList[0].RequestBody).GetAllNodes().Where(x => x.Name == "id").ToList(); AppModelParameters = AAMTempList[0].AppModelParameters.Where(x => x.TagName == "id").ToList(); AAMTempList = new JSONTemplateParser().ParseDocument(JsonFilePath, AAMTempList, true); jsonElementsAvoidDuplicatesNodes = new JsonExtended(AAMTempList[0].RequestBody).GetAllNodes().Where(x => x.Name == "id").ToList(); AppModelParametersAvoidDuplicatesNodes = AAMTempList[0].AppModelParameters.Where(x => x.TagName == "id").ToList(); //Assert Assert.AreEqual(jsonElements.Count, 499); Assert.AreEqual(jsonElementsAvoidDuplicatesNodes.Count, 1); Assert.AreEqual(AppModelParameters.Count, 499); Assert.AreEqual(AppModelParametersAvoidDuplicatesNodes.Count, 1); }
public static object[] GenerateBodyANdModelParameters(string JSOnText) { object[] BodyParamArray = new object[2]; JsonExtended JE = new JsonExtended(JSOnText); ObservableList <AppModelParameter> AppModelParameters = new ObservableList <AppModelParameter>(); JToken jt = JToken.Parse(JSOnText); Dictionary <string, string> ParamPath = new Dictionary <string, string>(); List <string> consts = new List <string>(); foreach (var Jn in JE.GetEndingNodes()) { if (Jn == null) { continue; } string tagName = Jn.Path.Split('.').LastOrDefault(); string paramname = tagName.ToUpper(); int i = 0; string param = "<" + paramname + ">"; while (ParamPath.ContainsKey(param)) { i++; param = "<" + paramname + i + ">"; } ParamPath.Add(param, Jn.Path); JToken jt2 = jt.SelectToken(Jn.Path); try { if (jt2.Type != JTokenType.String && jt2.Type != JTokenType.Array) { consts.Add(param); } ((JValue)jt2).Value = param; } catch (Exception) { if (jt2.Type != JTokenType.String && jt2.Type != JTokenType.Array) { consts.Add(param); } jt2.Replace(param); } AppModelParameters.Add(new AppModelParameter(param, "", tagName, Jn.Path, new ObservableList <OptionalValue>())); } string body = jt.ToString(); foreach (var item in consts) { body = body.Replace("\"" + item + "\"", item); } BodyParamArray[0] = body; BodyParamArray[1] = AppModelParameters; return(BodyParamArray); }
public void XMLDocGetAllNodesTest() { //Arrange string jsonfilepath = TestResources.GetTestResourcesFile(@"JSON\sample.json"); string JSOnText = System.IO.File.ReadAllText(jsonfilepath); //Act XDE = new JsonExtended(JSOnText); int nodesCount = XDE.GetAllNodes().Count; var endingNodes = XDE.GetEndingNodes(); //Assert Assert.AreEqual(12, nodesCount); }
public void GetJSONAllOptionalValuesFromExamplesFile(string fileContent, Dictionary <Tuple <string, string>, List <string> > optionalValuesPerParameterDict) { JsonExtended JE = new JsonExtended(fileContent); foreach (JsonExtended JTN in JE.GetEndingNodes()) { try { AddJSONValueToOptionalValuesPerParameterDict(optionalValuesPerParameterDict, JTN.GetToken()); } catch (Exception ex) { Reporter.ToLog(eLogLevel.ERROR, ex.StackTrace); } } }
private void GetJSONAllOptionalValuesFromExamplesFile(TemplateFile xMLTemplateFile, Dictionary <Tuple <string, string>, List <string> > optionalValuesPerParameterDict) { string FileContent = File.ReadAllText(xMLTemplateFile.FilePath); JsonExtended JE = new JsonExtended(FileContent); foreach (JsonExtended JTN in JE.GetEndingNodes()) { try { AddJSONValueToOptionalValuesPerParameterDict(optionalValuesPerParameterDict, JTN.GetToken()); } catch { } } }
public static ObservableList <ActReturnValue> ParseJSONResponseSampleIntoReturnValues(string JSOnText) { ObservableList <ActReturnValue> ReturnValues = new ObservableList <ActReturnValue>(); JToken jt = JToken.Parse(JSOnText); JsonExtended JE = new JsonExtended(JSOnText); foreach (var Jn in JE.GetEndingNodes()) { string tagName = Jn.Path.Split('.').LastOrDefault(); ReturnValues.Add(new ActReturnValue() { Param = tagName, Path = Jn.Path, Active = true, DoNotConsiderAsTemp = true }); } return(ReturnValues); }
public APIModelBodyNodeSyncPage(ApplicationAPIModel applicationAPIModel, List <AppModelParameter> paramsToDelete) { InitializeComponent(); mParamsPendingDelete = paramsToDelete; mApplicationAPIModel = applicationAPIModel; if (APIConfigurationsDocumentParserBase.IsValidXML(mApplicationAPIModel.RequestBody)) { requestBodyType = ApplicationAPIUtils.eContentType.XML; XMLDoc = new XmlDocument(); XMLDoc.LoadXml(mApplicationAPIModel.RequestBody); } else if (APIConfigurationsDocumentParserBase.IsValidJson(mApplicationAPIModel.RequestBody)) { requestBodyType = ApplicationAPIUtils.eContentType.JSon; JsonDoc = new JsonExtended(applicationAPIModel.RequestBody); } else { requestBodyType = ApplicationAPIUtils.eContentType.TextPlain; } }
private void JSONValidation(string json) { JToken jo = null; try { jo = JObject.Parse(json); } catch { jo = JArray.Parse(json); } foreach (ActInputValue aiv in DynamicElements) { ValueExpression VE = new ValueExpression(RunOnEnvironment, RunOnBusinessFlow, DSList); VE.Value = @aiv.Param; if (string.IsNullOrEmpty(VE.ValueCalculated)) { continue; } JToken Tokenfound = jo.SelectToken(VE.ValueCalculated); if (Tokenfound != null) { AddOrUpdateReturnParamActualWithPath("InnerText", Tokenfound.ToString(), VE.ValueCalculated); if (Tokenfound.Children().Count() > 0) { JsonExtended JE = new JsonExtended(Tokenfound.ToString()); foreach (JsonExtended item in JE.GetEndingNodes()) { AddOrUpdateReturnParamActualWithPath(item.Name, item.JsonString, item.Path); } } } } }
private static ObservableList <ApplicationAPIModel> GetParameters(string jsonText, ObservableList <ApplicationAPIModel> AAMSList, bool avoidDuplicatesNodes, string fileName) { ApplicationAPIModel AAM = new ApplicationAPIModel(); AAM.Name = Path.GetFileNameWithoutExtension(fileName); //JObject jo = JObject.Parse(JSOnText); //IList<string> keys = jo.Properties().Select(p => p.Path).ToList(); if (avoidDuplicatesNodes) { JsonExtended fullJSOnObjectExtended = new JsonExtended(jsonText); fullJSOnObjectExtended.RemoveDuplicatesNodes(); jsonText = fullJSOnObjectExtended.JsonString; } object[] BodyandModelParameters = GenerateBodyANdModelParameters(jsonText); AAM.RequestBody = (string)BodyandModelParameters[0]; AAM.AppModelParameters = (ObservableList <AppModelParameter>)BodyandModelParameters[1]; AAMSList.Add(AAM); return(AAMSList); }
public static object[] GenerateBodyANdModelParameters(string JSOnText) { object[] BodyParamArray = new object[2]; JsonExtended JE = new JsonExtended(JSOnText); ObservableList <AppModelParameter> AppModelParameters = new ObservableList <AppModelParameter>(); JToken jt = JToken.Parse(JSOnText); Dictionary <string, string> ParamPath = new Dictionary <string, string>(); List <string> consts = new List <string>(); IEnumerable <JsonExtended> EndingNodesList = JE.GetEndingNodes(); foreach (var Jn in EndingNodesList) { if (Jn == null) { continue; } string tagName = Jn.Path.Split('.').LastOrDefault(); string paramname = tagName.ToUpper(); int i = 0; string param = "<" + paramname + ">"; while (ParamPath.ContainsKey(param)) { i++; param = "<" + paramname + i + ">"; } ParamPath.Add(param, Jn.Path); JToken jt2 = jt.SelectToken(Jn.Path); try { if (jt2.Type != JTokenType.String && jt2.Type != JTokenType.Array) { consts.Add(param); } //handling empty aaray like 'NewCar':[ ] if (jt2.Type == JTokenType.Array && jt2.Children().Count() == 0) { string jsonarraystring = "[ ]"; jt2 = JArray.Parse(jsonarraystring); } else { ((JValue)jt2).Value = param; } } catch (Exception ex) { Console.WriteLine("GenerateBodyANdModelParameters error - " + ex.Message); // Why do we do work in exception !!!!!!!!!!!!! if (jt2.Type != JTokenType.String && jt2.Type != JTokenType.Array) { consts.Add(param); } jt2.Replace(param); } AppModelParameters.Add(new AppModelParameter(param, "", tagName, Jn.Path, new ObservableList <OptionalValue>())); } string body = jt.ToString(); foreach (var item in consts) { body = body.Replace("\"" + item + "\"", item); } BodyParamArray[0] = body; BodyParamArray[1] = AppModelParameters; return(BodyParamArray); }