SettingsBase refactoring

This commit is contained in:
Jaex 2019-08-21 16:49:46 +03:00
parent 6141994cfb
commit d33c7001c5

View file

@ -130,7 +130,7 @@ public static T Load(string filePath, string backupFolder = null, bool createBac
private bool SaveInternal(string filePath)
{
string typeName = GetType().Name;
DebugHelper.WriteLine("{0} save started: {1}", typeName, filePath);
DebugHelper.WriteLine($"{typeName} save started: {filePath}");
bool isSuccess = false;
@ -148,12 +148,11 @@ private bool SaveInternal(string filePath)
using (StreamWriter streamWriter = new StreamWriter(fileStream))
using (JsonTextWriter jsonWriter = new JsonTextWriter(streamWriter))
{
jsonWriter.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
jsonWriter.Formatting = Formatting.Indented;
JsonSerializer serializer = new JsonSerializer();
serializer.ContractResolver = new WritablePropertiesOnlyResolver();
serializer.Converters.Add(new StringEnumConverter());
serializer.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
serializer.Formatting = Formatting.Indented;
serializer.Serialize(jsonWriter, this);
jsonWriter.Flush();
}
@ -187,7 +186,8 @@ private bool SaveInternal(string filePath)
}
finally
{
DebugHelper.WriteLine("{0} save {1}: {2}", typeName, isSuccess ? "successful" : "failed", filePath);
string status = isSuccess ? "successful" : "failed";
DebugHelper.WriteLine($"{typeName} save {status}: {filePath}");
}
return isSuccess;
@ -199,7 +199,7 @@ private static T LoadInternal(string filePath, string backupFolder = null)
if (!string.IsNullOrEmpty(filePath))
{
DebugHelper.WriteLine("{0} load started: {1}", typeName, filePath);
DebugHelper.WriteLine($"{typeName} load started: {filePath}");
try
{
@ -214,20 +214,19 @@ private static T LoadInternal(string filePath, string backupFolder = null)
using (StreamReader streamReader = new StreamReader(fileStream))
using (JsonTextReader jsonReader = new JsonTextReader(streamReader))
{
jsonReader.DateTimeZoneHandling = DateTimeZoneHandling.Local;
JsonSerializer serializer = new JsonSerializer();
serializer.Converters.Add(new StringEnumConverter());
serializer.DateTimeZoneHandling = DateTimeZoneHandling.Local;
serializer.ObjectCreationHandling = ObjectCreationHandling.Replace;
settings = serializer.Deserialize<T>(jsonReader);
}
if (settings == null)
{
throw new Exception(typeName + " object is null.");
throw new Exception($"{typeName} object is null.");
}
DebugHelper.WriteLine("{0} load finished: {1}", typeName, filePath);
DebugHelper.WriteLine($"{typeName} load finished: {filePath}");
return settings;
}
@ -236,7 +235,7 @@ private static T LoadInternal(string filePath, string backupFolder = null)
}
catch (Exception e)
{
DebugHelper.WriteException(e, typeName + " load failed: " + filePath);
DebugHelper.WriteException(e, $"{typeName} load failed: {filePath}");
}
if (!string.IsNullOrEmpty(backupFolder))
@ -247,7 +246,7 @@ private static T LoadInternal(string filePath, string backupFolder = null)
}
}
DebugHelper.WriteLine("{0} not found. Loading new instance.", typeName);
DebugHelper.WriteLine($"{typeName} not found. Loading new instance.");
return new T();
}