-
Notifications
You must be signed in to change notification settings - Fork 0
/
Json.cs
220 lines (190 loc) · 7.89 KB
/
Json.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
namespace Pocoyo
{
public static class Json
{
public static string Serialize<T>(T data, bool formatted = true)
{
if (formatted)
{
return JsonConvert.SerializeObject(data, new JsonSerializerSettings
{
Formatting = Formatting.Indented,
});
}
return JsonConvert.SerializeObject(data);
}
public static string SerializeType<T>(T data, bool formatted = true)
{
var options = new JsonSerializerSettings
{
ContractResolver = new TypeOnlyContractResolver<T>(),
Formatting = formatted ? Formatting.Indented : Formatting.None
};
return JsonConvert.SerializeObject(data, options);
}
public static string SerializeListType<T>(List<T> data, bool formatted = true)
{
var options = new JsonSerializerSettings
{
ContractResolver = new ListTypeOnlyContractResolver<T>(),
Formatting = formatted ? Formatting.Indented : Formatting.None
};
return JsonConvert.SerializeObject(data, options);
}
public static bool SerializeToFile<T>(T data, string filePath, bool formatted = true)
{
try
{
var json = Serialize(data, formatted);
return SharedFile.WriteAllText(filePath, json);
}
catch (Exception ex)
{
Log.Exception(filePath, ex);
//throw new Exception(string.Format("Json parse error: {0}\r\n{1}\r\n", ex.Message, filePath));
}
return false;
}
public static T Deserialize<T>(string json, bool formatted = true, bool camelCase = false)
{
var idxNull = json.IndexOf('\0');
if (idxNull != -1)
json = json.Substring(0, idxNull);
var jsonSettings = new JsonSerializerSettings();
if (formatted)
jsonSettings.Formatting = Formatting.Indented;
if (camelCase)
jsonSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
return JsonConvert.DeserializeObject<T>(json, jsonSettings);
}
public static T Deserialize<T>(string json, string typeName, bool formatted = true, bool camelCase = false) where T: class
{
var idxNull = json.IndexOf('\0');
if (idxNull != -1)
json = json.Substring(0, idxNull);
var type = Type.GetType(typeName);
var jsonSettings = new JsonSerializerSettings();
if (formatted)
jsonSettings.Formatting = Formatting.Indented;
if (camelCase)
jsonSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
return JsonConvert.DeserializeObject(json, type, jsonSettings) as T;
}
public static object Deserialize(string json, string typeName, bool formatted = true, bool camelCase = false)
{
var idxNull = json.IndexOf('\0');
if (idxNull != -1)
json = json.Substring(0, idxNull);
var type = Type.GetType(typeName);
var jsonSettings = new JsonSerializerSettings();
if (formatted)
jsonSettings.Formatting = Formatting.Indented;
if (camelCase)
jsonSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
return JsonConvert.DeserializeObject(json, type, jsonSettings);
}
public static T DeserializeType<T>(string json, bool formatted = true)
{
var idxNull = json.IndexOf('\0');
if (idxNull != -1)
json = json.Substring(0, idxNull);
var options = new JsonSerializerSettings
{
ContractResolver = new TypeOnlyContractResolver<T>(),
Formatting = formatted ? Formatting.Indented : Formatting.None
};
return JsonConvert.DeserializeObject<T>(json, options);
}
public static dynamic DeserializeFromFile(string filePath)
{
var json = "";
try
{
if (!File.Exists(filePath))
return new JObject();
json = SharedFile.ReadAllText(filePath);
if (string.IsNullOrEmpty(json))
return new JObject();
var idxNull = json.IndexOf('\0');
if (idxNull != -1)
json = json.Substring(0, idxNull);
dynamic data = JObject.Parse(json);
return data;
}
catch (Exception ex)
{
Log.Warn("Json parse error: {0}\r\n{1}\r\n{2}\r\n", ex.Message, filePath, json, ex);
//throw new Exception(string.Format("Json parse error: {0}\r\n{1}\r\n", ex.Message, filePath));
return new JObject();
}
}
public static T DeserializeFromFile<T>(string filePath)
{
var json = "";
try
{
json = SharedFile.ReadAllText(filePath);
return Deserialize<T>(json);
}
catch (Exception ex)
{
Log.Error("Json parse error: {0}\r\n{1}\r\n{2}\r\n", ex.Message, filePath, json, ex);
throw;
}
}
public static T TryDeserializeFromFile<T>(string filePath) where T : class
{
var json = "";
try
{
json = SharedFile.ReadAllText(filePath);
return Deserialize<T>(json);
}
catch (Exception ex)
{
Log.Warn("Json parse error: {0}\r\n{1}\r\n{2}\r\n", ex.Message, filePath, json, ex);
return null;
}
}
}
public class TypeOnlyContractResolver<T> : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);
#if Diagnostic
var serialize = property.DeclaringType == typeof(T) || property.DeclaringType.DeclaringType == typeof(T);
if (serialize)
Log.Info("Serialize {0} {1}", member.Name, property.DeclaringType.FullName);
else
Log.Warn("Not serializing {0} {1}", member.Name, property.DeclaringType.FullName);
#endif
property.ShouldSerialize = instance => property.DeclaringType == typeof(T) || property.DeclaringType.DeclaringType == typeof(T);
return property;
}
}
public class ListTypeOnlyContractResolver<T> : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);
#if Diagnostic
var serialize = property.DeclaringType == typeof(T) || property.DeclaringType.DeclaringType == typeof(T);
if (serialize)
Log.Info("Serialize {0} {1}", member.Name, property.DeclaringType.FullName);
else
Log.Warn("Not serializing {0} {1}", member.Name, property.DeclaringType.FullName);
#endif
property.ShouldSerialize = instance => property.DeclaringType == typeof(T) || property.DeclaringType.DeclaringType == typeof(T) || property.DeclaringType == typeof(List<T>) || property.DeclaringType.DeclaringType == typeof(List<T>);
return property;
}
}
}