-
Notifications
You must be signed in to change notification settings - Fork 3
/
JsonFile.cs
108 lines (90 loc) · 3.96 KB
/
JsonFile.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using Newtonsoft.Json;
using System;
using System.IO;
using System.Threading.Tasks;
namespace JsonSettings
{
public static class JsonFile
{
public static void Save<T>(string fileName, T @object, Action<JsonSerializerSettings> updateSettings = null, EventHandler<Newtonsoft.Json.Serialization.ErrorEventArgs> error = null)
{
string folder = Path.GetDirectoryName(fileName);
if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
using (StreamWriter writer = File.CreateText(fileName))
{
JsonSerializerSettings settings = new JsonSerializerSettings()
{
Error = error,
Formatting = Formatting.Indented,
ContractResolver = new DataProtectionResolver()
};
updateSettings?.Invoke(settings);
string json = JsonConvert.SerializeObject(@object, settings);
writer.Write(json);
}
}
public async static Task SaveAsync<T>(string fileName, T @object, Action<JsonSerializerSettings> updateSettings = null, EventHandler<Newtonsoft.Json.Serialization.ErrorEventArgs> error = null)
{
string folder = Path.GetDirectoryName(fileName);
if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
using (StreamWriter writer = File.CreateText(fileName))
{
JsonSerializerSettings settings = new JsonSerializerSettings()
{
Error = error,
Formatting = Formatting.Indented,
ContractResolver = new DataProtectionResolver()
};
updateSettings?.Invoke(settings);
string json = JsonConvert.SerializeObject(@object, settings);
await writer.WriteAsync(json);
}
}
public static T LoadOrCreate<T>(string fileName) where T : new()
{
if (!File.Exists(fileName)) return new T();
return Load<T>(fileName);
}
public static T Load<T>(string fileName, Func<T> ifNotExists = null, EventHandler<Newtonsoft.Json.Serialization.ErrorEventArgs> error = null)
{
if (!File.Exists(fileName) && ifNotExists != null) return ifNotExists.Invoke();
using (StreamReader reader = File.OpenText(fileName))
{
JsonSerializerSettings settings = new JsonSerializerSettings()
{
Error = error,
ContractResolver = new DataProtectionResolver()
};
string json = reader.ReadToEnd();
if (!string.IsNullOrEmpty(json) && !json.Equals("null"))
{
return JsonConvert.DeserializeObject<T>(json, settings);
}
}
return default(T);
}
public async static Task<T> LoadOrCreateAsync<T>(string fileName) where T : new()
{
if (!File.Exists(fileName)) return new T();
return await LoadAsync<T>(fileName);
}
public async static Task<T> LoadAsync<T>(string fileName, Func<T> ifNotExists = null, EventHandler<Newtonsoft.Json.Serialization.ErrorEventArgs> error = null)
{
if (!File.Exists(fileName) && ifNotExists != null) return ifNotExists.Invoke();
using (StreamReader reader = File.OpenText(fileName))
{
JsonSerializerSettings settings = new JsonSerializerSettings()
{
Error = error,
ContractResolver = new DataProtectionResolver()
};
string json = await reader.ReadToEndAsync();
if (!json.Equals(string.Empty))
{
return JsonConvert.DeserializeObject<T>(json, settings);
}
}
return default(T);
}
}
}