コード例 #1
0
 public JsonDataContext(string dataPath)
 {
     _dataPath = dataPath;
     ReadFromJson();
     if (data == null)
     {
         data = new JsonDataFileWrapper();
         InitializeJson();
     }
 }
コード例 #2
0
 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);
     }
 }
コード例 #3
0
 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;
     }
 }