public List <ContactInformation> GetContactList() { try { GenericInstance genericInstance = new GenericInstance(); string fileName = genericInstance.getFileName(); if (!genericInstance.IsFileExists(fileName)) { genericInstance.CreateFileIfNotExists(fileName); } JObject jsonObj = genericInstance.ReadFromFile(fileName); var local = jsonObj["ContactInformation"]; if (jsonObj != null) { return(local.ToObject <List <ContactInformation> >()); } else { return(new List <ContactInformation>()); } } catch (Exception ex) { throw ex; } }
public void AddContact(ContactInformation contactInformation) { GenericInstance genericInstance = new GenericInstance(); try { string fileName = genericInstance.getFileName(); if (!genericInstance.IsFileExists(fileName)) { genericInstance.CreateFileIfNotExists(fileName); } JObject jsonObj = genericInstance.ReadFromFile(fileName); JArray contactInfoArrary = new JArray(); if (jsonObj != null) { contactInfoArrary = jsonObj.GetValue("ContactInformation") as JArray; contactInformation.ContactID = genericInstance.GetMaxID(contactInfoArrary) + 1; } else { contactInformation.ContactID = 1; jsonObj = new JObject(); } var newContactJSonString = JsonConvert.SerializeObject(contactInformation); var newContact = JObject.Parse(newContactJSonString); contactInfoArrary.Add(newContact); jsonObj["ContactInformation"] = contactInfoArrary; string newJsonResult = JsonConvert.SerializeObject(jsonObj, Formatting.Indented); genericInstance.WriteToFile(fileName, newJsonResult); } catch (Exception ex) { throw ex; } }