internal static bool JsonObjectSerialize <T>(string saveDir, string filePath, ref T serializable, DoBackup doBackup) { CreateDirIfDoesntExist(saveDir); CreateBackupIfNeeded(filePath, doBackup); TextWriter writer = null; bool isSaveSuccessful = false; try { string output = JsonConvert.SerializeObject(serializable); writer = new StreamWriter(filePath, false); writer.Write(output); isSaveSuccessful = true; } catch (Exception ex) { logger.Error(ex, "Serialization failed!"); } finally { if (writer != null) { writer.Close(); } } return(isSaveSuccessful); }
private static void CreateBackupIfNeeded(string path, DoBackup doBackup) { if (DoBackup.Yes == doBackup && File.Exists(path)) { string backupPath = path + ".bak"; if (File.Exists(backupPath)) { File.Delete(backupPath); } File.Copy(path, backupPath); } }
internal static void JsonObjectSerialize <T>(string saveDir, string fileName, ref T serializable, DoBackup doBackup) { string path = CreateSavePath(saveDir, fileName); CreateDirIfDoesntExist(saveDir); CreateBackupIfNeeded(path, doBackup); TextWriter writer = null; try { string output = JsonConvert.SerializeObject(serializable); writer = new StreamWriter(path, false); writer.Write(output); } finally { if (writer != null) { writer.Close(); } } }