/* Here is the function for importing a .json file */ static Debt[] Import_json(string file, int entries, Debt[] debts) { ILogger logger = LogManager.GetCurrentClassLogger(); /* Log things, in case they go wrong */ logger.Info("Now we use the Import_json function"); /* Obtain the raw data */ try { /* Try to read the data. If not, the function will cancel and return what you started with. */ List <json_Debt> json_result; using (StreamReader r = new StreamReader(file)) { string json = r.ReadToEnd(); json_result = JsonConvert.DeserializeObject <List <json_Debt> >(json); } logger.Info("We've read the file"); json_Debt[] json_debts = new json_Debt[json_result.Count]; Debt[] converted_debts = new Debt[json_result.Count]; for (int i = 0; i < json_result.Count; i++) { json_debts[i] = json_result[i]; converted_debts[i] = new Debt(json_debts[i]); } /* Create a new array to store all the new data, alongside the old data */ Debt[] newdebts = new Debt[entries + json_result.Count]; for (int i = 0; i < entries; i++) { newdebts[i] = debts[i]; } /* Read through line by line, and add in all the new data */ for (int i = 0; i < json_result.Count; i++) { newdebts[i + entries] = converted_debts[i]; } logger.Info("I mean hopefully now it's done, right?"); return(newdebts); } catch { Console.WriteLine("Invalid file. Please try again."); return(debts); } }
/* We also here have a construct that will take the Json form of a debt, and convert it to the standard form */ public Debt(json_Debt raw) { Date = raw.Date; From = raw.FromAccount; To = raw.ToAccount; Narrative = raw.Narrative; Amount = raw.Amount; }