public JsonDataContext(string dataPath) { _dataPath = dataPath; ReadFromJson(); if (data == null) { data = new JsonDataFileWrapper(); InitializeJson(); } }
private void WriteToJson(JsonDataFileWrapper output) { try { using StreamWriter sw = new StreamWriter(_dataPath); sw.Write(JsonConvert.SerializeObject(output, Formatting.Indented)); } catch (Exception ex) { throw new IOException("was not able to write to the data file.", ex); } }
private void ReadFromJson() { try { using StreamReader sr = new StreamReader(_dataPath); data = JsonConvert.DeserializeObject <JsonDataFileWrapper>(sr.ReadToEnd()); } catch (Exception ex) { Exception dataExcepion = new FileLoadException("data file could not be read. invalid file/format.", ex); data = new JsonDataFileWrapper(); InitializeJson(); ReadFromJson(); throw dataExcepion; } }