internal static void Serialize(AppSettings appSettings) { if (!Directory.Exists(FilePathSettings.DataSavePath)) { Directory.CreateDirectory(FilePathSettings.DataSavePath); } File.WriteAllText(SavePath, DataEncryption.Encrypt(JsonConvert.SerializeObject(appSettings))); }
internal static void Serialize(List <IdleTimer> timerCollection) { if (!Directory.Exists(FilePathSettings.DataSavePath)) { Directory.CreateDirectory(FilePathSettings.DataSavePath); } File.WriteAllText(SavePath, DataEncryption.Encrypt(JsonConvert.SerializeObject(timerCollection))); }
public T DeSerialize(string encryptedString) { T obj; try { var text = DataEncryption.Decrypt(encryptedString); obj = JsonConvert.DeserializeObject <T>(text); } catch (Exception) { obj = new T(); } return(obj); }
/// <exception cref="SerializerError">Something has gone wrong with serialization, message contain more information</exception> public void Serialize(T obj) { if (savePath == null) { throw new SerializerError("No Save Path"); } try { File.WriteAllText(savePath, DataEncryption.Encrypt(JsonConvert.SerializeObject(obj))); } catch (Exception ex) { throw new SerializerError("Error in serialization", ex); } }
private T DeSerialize(string encryptedString, int retryCount) { try { var text = DataEncryption.Decrypt(encryptedString); return(JsonConvert.DeserializeObject <T>(text)); } catch (Exception) { if (retryCount >= 3) { return(new T()); } Thread.Sleep(100); return(DeSerialize(encryptedString, retryCount + 1)); } }
private void Serialize(T obj, int retryCount) { if (savePath == null) { throw new SerializerError("No Save Path"); } try { if (File.Exists(savePath)) { File.Copy(savePath, TempWritePath, true); if (!File.Exists(BackupPath) || (new FileInfo(BackupPath).LastAccessTime.Date != DateTime.Now.Date)) { File.Copy(savePath, BackupPath, true); } } File.WriteAllText(savePath, DataEncryption.Encrypt(JsonConvert.SerializeObject(obj))); } catch (Exception ex) { if (retryCount >= 3) { throw new SerializerError("Error in serialization", ex); } Thread.Sleep(100); Serialize(obj, retryCount + 1); } finally { try { if (File.Exists(TempWritePath)) { File.Delete(TempWritePath); } } catch (Exception) { //ignored } } }
private T DeSerialize(string fileToUse, int retryCount, int backupNumber) { if (fileToUse == null) { throw new SerializerError("No Save Path"); } if (!File.Exists(fileToUse)) { return(new T()); } singleThreadMutex.WaitOne(); try { var encryptedString = File.ReadAllText(fileToUse); var text = DataEncryption.Decrypt(encryptedString, encryptionPassPhrase); if (string.IsNullOrWhiteSpace(text)) { throw new Exception("String in file is null or blank"); } return(JsonConvert.DeserializeObject <T>(text)); } catch (Exception ex) { if (retryCount >= 3) { File.WriteAllText(Path.Combine(errorDirectory, $"DeSerializeFile_{DateTime.UtcNow:yyyy-MM-dd_hh-mm-ss}_{Guid.NewGuid()}.log"), ex.ToString()); var nextFile = backupNumber == 0 ? $"{backupPath}.bak" : $"{backupPath}.{backupNumber}.bak"; return(DeSerialize(nextFile, 0, backupNumber + 1)); } Thread.Sleep(200); return(DeSerialize(fileToUse, retryCount + 1, backupNumber)); } finally { singleThreadMutex.ReleaseMutex(); } }
private void Serialize(T obj, int retryCount) { if (savePath == null) { throw new SerializerError("No Save Path"); } try { File.WriteAllText(savePath, DataEncryption.Encrypt(JsonConvert.SerializeObject(obj))); } catch (Exception ex) { if (retryCount >= 3) { throw new SerializerError("Error in serialization", ex); } Thread.Sleep(100); Serialize(obj, retryCount + 1); } }
internal static List <IdleTimer> DeSerialize() { List <IdleTimer> timers; if (File.Exists(SavePath)) { try { var text = DataEncryption.Decrypt(File.ReadAllText(SavePath)); timers = JsonConvert.DeserializeObject <List <IdleTimer> >(text); } catch (Exception) { timers = new List <IdleTimer>(); } } else { timers = new List <IdleTimer>(); } return(timers); }
internal static AppSettings DeSerialize() { AppSettings appSettings; if (File.Exists(SavePath)) { try { var text = DataEncryption.Decrypt(File.ReadAllText(SavePath)); appSettings = JsonConvert.DeserializeObject <AppSettings>(text); } catch (Exception) { appSettings = new AppSettings(); } } else { appSettings = new AppSettings(); } return(appSettings); }
internal static JiraConnectionSettings DeSerialize() { JiraConnectionSettings jiraConnectionSettings; if (File.Exists(SavePath)) { try { var text = DataEncryption.Decrypt(File.ReadAllText(SavePath)); jiraConnectionSettings = JsonConvert.DeserializeObject <JiraConnectionSettings>(text); } catch (Exception) { jiraConnectionSettings = new JiraConnectionSettings(); } } else { jiraConnectionSettings = new JiraConnectionSettings(); } return(jiraConnectionSettings); }
private void Serialize(T obj, int retryCount) { if (savePath == null) { throw new SerializerError("No Save Path"); } singleThreadMutex.WaitOne(); var tempWritePath = $"{backupPath}.temp.bak"; var mainBackupPath = $"{backupPath}.bak"; try { if (File.Exists(savePath)) { if (!File.Exists(tempWritePath)) { File.Copy(savePath, tempWritePath, true); } if (!File.Exists(mainBackupPath)) { File.Copy(savePath, mainBackupPath, true); } else if (File.GetLastWriteTimeUtc(mainBackupPath) < DateTime.UtcNow.AddHours(-1)) { var rollingBackupPath = $"{backupPath}.24.bak"; if (File.Exists(rollingBackupPath)) { File.Delete(rollingBackupPath); } for (var i = 23; i > 0; i--) { rollingBackupPath = $"{backupPath}.{i}.bak"; if (File.Exists(rollingBackupPath)) { File.Move(rollingBackupPath, $"{backupPath}.{i + 1}.bak"); } } File.Move(mainBackupPath, $"{backupPath}.1.bak"); File.Copy(savePath, mainBackupPath, true); } } File.WriteAllText(savePath, DataEncryption.EncryptCaseInsensitive(JsonConvert.SerializeObject(obj), encryptionPassPhrase)); } catch (Exception ex) { if (retryCount >= 3) { if (File.Exists(tempWritePath)) { File.Copy(tempWritePath, savePath, true); } File.WriteAllText(Path.Combine(errorDirectory, $"Serialize_{DateTime.UtcNow:yyyy-MM-dd_hh-mm-ss}_{Guid.NewGuid().ToString()}.log"), ex.ToString()); throw new SerializerError("Error in serialization", ex); } Thread.Sleep(500); Serialize(obj, retryCount + 1); } finally { try { if (File.Exists(tempWritePath)) { File.Delete(tempWritePath); } } catch (Exception) { //ignored } singleThreadMutex.ReleaseMutex(); } }