-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJson.cs
316 lines (277 loc) · 9.95 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using CEM.World;
namespace CEM.Client {
/// <summary>
/// Simple JSON Conversion Utility
/// @author mlinder
/// </summary>
internal class Json {
/// <summary>
/// Default Json instance
/// </summary>
public static readonly Json Default = new Json() { SerializeFieldNames = true };
public Json() {
SerializeFieldNames = true;
Pretty = false;
}
#region Object -> Json
/// <summary>
/// Converts the specified object into an json string
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public string ToJson(object o) {
if (o == null)
return "null";
Type type = o.GetType();
switch (Type.GetTypeCode(type)) {
case TypeCode.Boolean:
return (bool) o ? "true" : "false";
case TypeCode.String:
case TypeCode.Char:
case TypeCode.DateTime:
return "\"" + EscapeString(Convert.ToString(o, CultureInfo.InvariantCulture)) + "\"";
case TypeCode.DBNull:
case TypeCode.Empty:
throw new NotImplementedException();
case TypeCode.Object:
return ObjectToJson(type, o);
default: // Number
if (type.IsEnum)
return ((int) o).ToString();
return Convert.ToString(o, CultureInfo.InvariantCulture);
}
}
private string ObjectToJson(Type type, object obj) {
var str = new StringBuilder();
if (type.IsArray) {
str.Append("[");
foreach (var o in (Array) obj) {
if (str.Length > 1)
str.Append(",");
str.Append(ToJson(o));
}
str.Append("]");
return str.ToString();
}
// If this is a class/struct, only serialize fields
str.Append("{");
if (Pretty) str.Append(Environment.NewLine);
int i = 0;
foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.Instance)) {
if (str.Length > 1)
str.Append(",");
str.Append(JsonField((SerializeFieldNames ? field.Name : (object) i), field.GetValue(obj)));
if (Pretty) str.Append(Environment.NewLine);
i++;
}
foreach (var prop in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)) {
if (prop.GetIndexParameters().Any())
continue; // Skip arrays
if (str.Length > 1)
str.Append(",");
str.Append(JsonField((SerializeFieldNames ? prop.Name : (object) i), prop.GetValue(obj, null)));
if (Pretty) str.Append(Environment.NewLine);
i++;
}
str.Append("}");
if (Pretty) str.Append(Environment.NewLine);
return str.ToString();
}
private string JsonField(object name, object value) {
return ToJson(name) + ":" + ToJson(value);
}
private static string EscapeString(string str) {
str = str.Replace("\\", "\\\\"); // replace \ with \\
str = str.Replace("\"", "\\\""); // replace " with \"
return str;
}
#endregion
#region Json -> Object
/// <summary>
/// Parses the specified object as an json string
/// </summary>
public T ParseJson<T>(string json) {
object res = ParseJson(json, typeof (T));
return res == null ? default(T) : T<T>(res);
}
/// <summary>
/// Parses the specified object as an json string
/// </summary>
public object ParseJson(string json, Type type) {
if (json == "null" || json == null)
return null;
json = json.Trim();
switch (Type.GetTypeCode(type)) {
case TypeCode.DateTime:
return DateTime.Parse(ParseJsonString(json), CultureInfo.InvariantCulture);
case TypeCode.Object:
if (Nullable.GetUnderlyingType(type) != null)
return ParseJson(json, Nullable.GetUnderlyingType(type));
if (type.IsArray)
return ParseArray(json, type.GetElementType());
if (type == typeof (object) && !json.StartsWith("{")) {
// value type
if (json.StartsWith("\"")) return ParseJson(json, typeof (string));
if (json.StartsWith("[")) return ParseJson(json, typeof (object[]));
if (json.Contains(".")) return ParseJson(json, typeof (double));
if (json == "true" || json == "false") return ParseJson(json, typeof (bool));
return ParseJson(json, typeof (int));
}
return ParseClass(json, type);
case TypeCode.String:
return ParseJsonString(json);
default:
if (type.IsEnum)
return Enum.Parse(type, json.Trim('"', ' '));
return Convert.ChangeType(json, type);
}
}
/// <summary>
/// Parses the specified object as an json string
/// </summary>
public void ParseJson(string json, ref object output) {
output = ParseJson(json, output.GetType());
}
/// <summary>
/// Parses the specified object as an json string
/// </summary>
public void ParseJson(Stream stream, ref object output)
{
output = ParseJson(new StreamReader(stream).ReadToEnd(), output.GetType());
}
/// <summary>
/// Parses the specified object as an json string
/// </summary>
public T ParseJson<T>(Stream json)
{
return ParseJson<T>(new StreamReader(json).ReadToEnd());
}
private object ParseClass(string json, Type clazz) {
if (!json.StartsWith("{") || !json.EndsWith("}"))
throw new ArgumentException("Expected class but got: " + json);
json = json.Substring(1, json.Length - 2);
if (clazz == typeof (object)) clazz = typeof (Hashtable);
object instance = Activator.CreateInstance(clazz);
foreach (var fieldjson in FindTokens(json, ',')) {
string[] field = FindTokens(fieldjson, ':').ToArray();
var key = ParseJson<object>(field[0]);
string value = field[1];
// Check if we have a field with that name
FieldInfo fi;
PropertyInfo pi;
if ((fi = GetField(clazz, key)) != null) {
fi.SetValue(instance, ParseJson(value, fi.FieldType));
}
else if ((pi = GetProperty(clazz, key)) != null) {
pi.SetValue(instance, ParseJson(value, pi.PropertyType), null);
}
else if (typeof (Hashtable).IsAssignableFrom(clazz)) {
((Hashtable) instance).Add(key, ParseJson<object>(value));
}
}
return instance;
}
private static FieldInfo GetField(Type clazz, object key) {
if (key is string) {
return clazz.GetField((string) key, BindingFlags.Instance | BindingFlags.Public);
}
if (key is int) {
FieldInfo[] fields = clazz.GetFields(BindingFlags.Instance | BindingFlags.Public);
if ((int) key >= fields.Length)
return null;
return fields[(int) key];
}
return null;
}
private static PropertyInfo GetProperty(Type clazz, object key) {
if (key is string) {
return clazz.GetProperty((string) key, BindingFlags.Instance | BindingFlags.Public);
}
if (key is int) {
PropertyInfo[] props = clazz.GetProperties(BindingFlags.Instance | BindingFlags.Public);
int ind = (int) key - clazz.GetFields(BindingFlags.Instance | BindingFlags.Public).Length;
if (ind >= props.Length)
return null;
return props[ind];
}
return null;
}
private Array ParseArray(string json, Type valueType) {
if (!json.StartsWith("[") || !json.EndsWith("]"))
throw new ArgumentException("Expected array but got: " + json);
json = json.Substring(1, json.Length - 2);
var list = new ArrayList();
foreach (var token in FindTokens(json, ','))
list.Add(ParseJson(token, valueType));
return list.ToArray(valueType);
}
/// <summary>
/// Find tokens on the same logic level (brackets, ..)
/// </summary>
private IEnumerable<string> FindTokens(string json, char separator) {
int tokenStart = 0;
int? tokenEnd;
do {
tokenEnd = FindNextToken(json, tokenStart, separator);
string token = json.Substring(tokenStart, (tokenEnd ?? json.Length) - tokenStart).Trim();
if (!string.IsNullOrEmpty(token))
yield return token;
if (tokenEnd != null) {
tokenStart = (int) tokenEnd + 1;
}
} while (tokenEnd != null);
}
private int? FindNextToken(string str, int startPos, char token) {
// Find the next non-string token
bool inString = false;
bool escape = false;
int bracketLevel = 0;
char c;
do {
if (startPos >= str.Length)
return null;
c = str[startPos++];
if (inString) {
if (escape) escape = false;
else if (c == '\\') escape = true;
else if (c == '"') inString = false;
}
else {
if (c == '"') inString = true;
else if (c == '{' || c == '[') bracketLevel++;
else if (c == '}' || c == ']') bracketLevel--;
}
} while ((c != token || inString || bracketLevel != 0));
return startPos - 1;
}
private TK T<TK>(object o) {
Type type = typeof (TK);
return type == typeof (object) ? (TK) Convert.ChangeType(o, o.GetType()) : (TK) Convert.ChangeType(o, type);
}
private string ParseJsonString(string json) {
if (!json.StartsWith("\"") || !json.EndsWith("\""))
throw new ArgumentException("Expected string but got: " + json);
json = json.Substring(1, json.Length - 2);
json = json.Replace("\\\"", "\"");
json = json.Replace("\\\\", "\\");
return json;
}
#endregion
/// <summary>
/// True if field names are serialized as strings, or false to use indexes only
/// </summary>
public bool SerializeFieldNames { get; set; }
/// <summary>
/// True to use pretty formatting
/// </summary>
public bool Pretty { get; set; }
}
}