-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonFileSerializer.cs
45 lines (32 loc) · 1.13 KB
/
JsonFileSerializer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using Newtonsoft.Json;
using System.IO;
namespace Zenvin.Settings.Framework.Serialization {
/// <summary>
/// Extension of <see cref="JsonSerializer"/>, which automatically writes changes to a file post serialization.
/// </summary>
public sealed class JsonFileSerializer : JsonSerializer, ISerializerCallbackReceiver {
public FileInfo SaveFile { get; private set; }
public Formatting OutputFormatting { get; set; } = Formatting.Indented;
public JsonConverter[] Converters { get; set; }
public JsonFileSerializer (string filePath) {
SaveFile = new FileInfo (filePath);
}
public JsonFileSerializer (FileInfo file) {
SaveFile = file;
}
public bool ReadFromFile () {
return ReadFromFile (SaveFile);
}
public bool WriteToFile () {
return WriteToFile (SaveFile, OutputFormatting, Converters);
}
void ISerializerCallbackReceiver.FinalizeSerialization () {
WriteToFile ();
}
void ISerializerCallbackReceiver.InitializeDeserialization () {
ReadFromFile ();
}
void ISerializerCallbackReceiver.FinalizeDeserialization () { }
void ISerializerCallbackReceiver.InitializeSerialization () { }
}
}