/// <summary> /// Deserializes a singular Project with children objects. /// </summary> /// <param name="jsonString">Serialized data contain information about project and its children</param> /// <returns>A deserialized Project object</returns> private Project Deserialize(string jsonString) { DbProject db = new DbProject(); Dictionary <string, object> dict = JsonConvert.DeserializeObject <Dictionary <string, object> >(jsonString); System.Diagnostics.Debug.WriteLine("DeserializeApiData. Printing Key Value:"); string[] keys = dict.Keys.ToArray(); Project p = new Project(); p.companies = new List <Company>(); p.courses = new List <Course>(); p.studyGroups = new List <StudyGroup>(); p.jobTypes = new List <JobType>(); foreach (var key in keys) { System.Diagnostics.Debug.WriteLine("key: " + key); System.Diagnostics.Debug.WriteLine("value: " + dict[key].ToString()); /* * if (!key.Equals("companies") || !key.Equals("courses") || !key.Equals("degrees") || !key.Equals("jobTypes") || !key.Equals("studyGroup")) {} */ if (key.Equals("uuid")) { p.uuid = dict[key].ToString(); } if (key.Equals("title")) { p.title = dict[key].ToString(); } /* * if (key.Equals("description")) * { * p.description = dict[key].ToString(); * } */ if (key.Equals("webpage")) { p.webpage = dict[key].ToString(); } if (key.Equals("published")) { DateTime dateTime = (DateTime)dict[key]; p.published = long.Parse(dateTime.ToString("yyyyMMddHHmmss")); } if (key.Equals("modified")) { DateTime dateTime = (DateTime)dict[key]; p.modified = long.Parse(dateTime.ToString("yyyyMMddHHmmss")); } if (key.Equals("companies")) { // if not true then company already exist and needs to be updated. CompaniesController cc = new CompaniesController(); DbCompany dbCompany = new DbCompany(); IEnumerable companies = (IEnumerable)dict[key]; //`Newtonsoft.Json.Linq.JArray' System.Diagnostics.Debug.WriteLine("companies created"); foreach (var comp in companies) { System.Diagnostics.Debug.WriteLine("foreach initiated"); Dictionary <string, object> companyDict = JsonConvert.DeserializeObject <Dictionary <string, object> >(comp.ToString()); Company company = cc.DeserializeCompany(companyDict); System.Diagnostics.Debug.WriteLine("Deserialize: company.id: " + company.id); p.companies.Add(company); dbCompany.UpdateCompany(company); System.Diagnostics.Debug.WriteLine("Deserialize: After j.companies.Add(company)"); string projectUuid = dict["uuid"].ToString(); dbCompany.InsertCompanyProject(company.id, projectUuid); } } if (key.Equals("courses")) { DbCourse dbCourse = new DbCourse(); IEnumerable courses = (IEnumerable)dict[key]; //Newtonsoft.Json.Linq.JArray' System.Diagnostics.Debug.WriteLine("location created"); foreach (var course in courses) { System.Diagnostics.Debug.WriteLine("foreach initiated"); Dictionary <string, object> courseDict = JsonConvert.DeserializeObject <Dictionary <string, object> >(course.ToString()); Course co = new Course(); if (courseDict.ContainsKey("id")) { co.id = courseDict["id"].ToString(); System.Diagnostics.Debug.WriteLine("Course id: " + co.id); } if (courseDict.ContainsKey("name")) { co.name = courseDict["name"].ToString(); } dbCourse.InsertCourse(co); p.courses.Add(co); string projectUuid = dict["uuid"].ToString(); dbCourse.InsertCourseProject(co.id, projectUuid); } } if (key.Equals("studyGroups")) { DbStudyGroup dbStudyGroup = new DbStudyGroup(); IEnumerable studyGroups = (IEnumerable)dict[key]; //Newtonsoft.Json.Linq.JArray' System.Diagnostics.Debug.WriteLine("studyGroups created"); foreach (var studyGroup in studyGroups) { System.Diagnostics.Debug.WriteLine("foreach initiated"); Dictionary <string, object> studyGroupDict = JsonConvert.DeserializeObject <Dictionary <string, object> >(studyGroup.ToString()); StudyGroup sg = new StudyGroup(); if (studyGroupDict.ContainsKey("id")) { sg.id = studyGroupDict["id"].ToString(); } if (studyGroupDict.ContainsKey("name")) { sg.name = studyGroupDict["name"].ToString(); } p.studyGroups.Add(sg); string projectUuid = dict["uuid"].ToString(); dbStudyGroup.InsertStudyGroupProject(sg.id, projectUuid); } } /* * if (key.Equals("approvedCourses")) * { * * Same as companies implementation * * } * * if (key.Equals("degrees")) * { * * Same as companies implementation * * } */ if (key.Equals("jobTypes")) { DbJobType dbJobType = new DbJobType(); IEnumerable jobTypes = (IEnumerable)dict[key]; //Newtonsoft.Json.Linq.JArray' System.Diagnostics.Debug.WriteLine("jobTypes created"); foreach (var jobType in jobTypes) { System.Diagnostics.Debug.WriteLine("foreach initiated"); Dictionary <string, object> jtDict = JsonConvert.DeserializeObject <Dictionary <string, object> >(jobType.ToString()); JobType jt = new JobType(); if (jtDict.ContainsKey("id")) { jt.id = jtDict["id"].ToString(); } if (jtDict.ContainsKey("name")) { jt.name = jtDict["name"].ToString(); } dbJobType.InsertJobType(jt); System.Diagnostics.Debug.WriteLine("before p.jobTypes.Add(jt);"); p.jobTypes.Add(jt); string projectUuid = dict["uuid"].ToString(); dbJobType.InsertJobTypeProject(jt.id, projectUuid); } } } db.UpdateProject(p); return(p); }