JsonSchemaValidatorFactory.cs
11.8 KB
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
317
318
319
320
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
#if UNIJSON_PROFILING
#else
using UnityEngine;
#endif
namespace UniJSON
{
public static class JsonSchemaValidatorFactory
{
struct JsonSchemaItem
{
public string Key;
public JsonSchema Schema;
public bool Required;
public string[] Dependencies;
}
static IEnumerable<JsonSchemaItem> GetProperties(Type t, PropertyExportFlags exportFlags)
{
// fields
foreach (var fi in t.GetFields())
{
var a = fi.GetCustomAttributes(typeof(JsonSchemaAttribute), true).FirstOrDefault() as JsonSchemaAttribute;
if (a == null)
{
a = fi.FieldType.GetCustomAttributes(typeof(JsonSchemaAttribute), true).FirstOrDefault() as JsonSchemaAttribute;
if (a == null)
{
// default
if (!fi.IsStatic && fi.IsPublic)
{
// only public instance field
a = new JsonSchemaAttribute();
}
}
}
// for array item
var ia = fi.GetCustomAttributes(typeof(ItemJsonSchemaAttribute), true).FirstOrDefault() as ItemJsonSchemaAttribute;
if (a == null)
{
//int x = 0;
}
else
{
yield return new JsonSchemaItem
{
Key = fi.Name,
Schema = JsonSchema.FromType(fi.FieldType, a, ia),
Required = a.Required,
Dependencies = a.Dependencies,
};
}
}
// properties
foreach (var pi in t.GetProperties())
{
var a = pi.GetCustomAttributes(typeof(JsonSchemaAttribute), true).FirstOrDefault() as JsonSchemaAttribute;
// for array item
var ia = pi.GetCustomAttributes(typeof(ItemJsonSchemaAttribute), true).FirstOrDefault() as ItemJsonSchemaAttribute;
if (a != null)
{
yield return new JsonSchemaItem
{
Key = pi.Name,
Schema = JsonSchema.FromType(pi.PropertyType, a, ia),
Required = a.Required,
Dependencies = a.Dependencies,
};
}
}
}
public static IJsonSchemaValidator Create(ValueNodeType valueType,
Type t = null,
BaseJsonSchemaAttribute a = null,
ItemJsonSchemaAttribute ia = null)
{
switch (valueType)
{
case ValueNodeType.Integer:
{
var v = new JsonIntValidator();
if (a != null)
{
if (!double.IsNaN(a.Minimum))
{
v.Minimum = (int)a.Minimum;
}
if (a.ExclusiveMinimum)
{
v.ExclusiveMinimum = a.ExclusiveMinimum;
}
if (!double.IsNaN(a.Maximum))
{
v.Maximum = (int)a.Maximum;
}
if (a.ExclusiveMaximum)
{
v.ExclusiveMaximum = a.ExclusiveMaximum;
}
if (a.MultipleOf != 0)
{
v.MultipleOf = (int)a.MultipleOf;
}
}
return v;
}
case ValueNodeType.Number:
{
var v = new JsonNumberValidator();
if (a != null)
{
if (!double.IsNaN(a.Minimum))
{
v.Minimum = (int)a.Minimum;
}
if (a.ExclusiveMinimum)
{
v.ExclusiveMinimum = a.ExclusiveMinimum;
}
if (!double.IsNaN(a.Maximum))
{
v.Maximum = (int)a.Maximum;
}
if (a.ExclusiveMaximum)
{
v.ExclusiveMaximum = a.ExclusiveMaximum;
}
if (a.MultipleOf != 0)
{
v.MultipleOf = (int)a.MultipleOf;
}
}
return v;
}
case ValueNodeType.String:
{
var v = new JsonStringValidator();
if (a != null)
{
if (a.Pattern != null)
{
v.Pattern = new System.Text.RegularExpressions.Regex(a.Pattern);
}
}
return v;
}
case ValueNodeType.Boolean:
return new JsonBoolValidator();
case ValueNodeType.Array:
{
var v = new JsonArrayValidator();
if (a != null)
{
if (a.MinItems != 0)
{
v.MinItems = a.MinItems;
}
if (a.MaxItems != 0)
{
v.MaxItems = a.MaxItems;
}
if (t != null)
{
if (ia == null)
{
ia = new ItemJsonSchemaAttribute();
}
Type elementType = null;
if (t.IsArray)
{
elementType = t.GetElementType();
}
else if (t.GetIsGenericList())
{
elementType = t.GetGenericArguments().First();
}
if (elementType != null)
{
/*
var sub = new JsonSchema
{
SkipComparison = ia.SkipSchemaComparison,
Validator = Create(elementType, ia, null)
};
*/
var sub = JsonSchema.FromType(elementType, ia, null);
v.Items = sub;
}
}
}
return v;
}
case ValueNodeType.Object:
{
if (t.GetIsGenericDictionary())
{
var genericFactory = typeof(JsonDictionaryValidator).GetMethod("Create", BindingFlags.Static | BindingFlags.Public);
var factory = genericFactory.MakeGenericMethod(t.GetGenericArguments()[1]);
var v = factory.Invoke(null, null) as IJsonSchemaValidator;
return v;
}
else
{
var v = new JsonObjectValidator();
if (a != null)
{
if (a.MinProperties > 0)
{
v.MinProperties = a.MinProperties;
}
// props
foreach (var prop in GetProperties(t, a.ExportFlags))
{
v.Properties.Add(prop.Key, prop.Schema);
if (prop.Required)
{
v.Required.Add(prop.Key);
}
if (prop.Dependencies != null)
{
v.Dependencies.Add(prop.Key, prop.Dependencies);
}
}
}
if (ia != null)
{
var sub = new JsonSchema
{
SkipComparison = ia.SkipSchemaComparison,
Validator = Create(typeof(object), ia, null)
};
v.AdditionalProperties = sub;
}
return v;
}
}
default:
throw new NotImplementedException();
}
}
public static IJsonSchemaValidator Create(string t)
{
return Create((ValueNodeType)Enum.Parse(typeof(ValueNodeType), t, true));
}
static Dictionary<Type, ValueNodeType> s_typeMap = new Dictionary<Type, ValueNodeType>
{
{typeof(byte), ValueNodeType.Integer },
{typeof(short), ValueNodeType.Integer },
{typeof(int), ValueNodeType.Integer },
{typeof(long), ValueNodeType.Integer },
{typeof(sbyte), ValueNodeType.Integer },
{typeof(ushort), ValueNodeType.Integer },
{typeof(uint), ValueNodeType.Integer },
{typeof(ulong), ValueNodeType.Integer },
{typeof(float), ValueNodeType.Number },
{typeof(double), ValueNodeType.Number },
{typeof(string), ValueNodeType.String },
{typeof(bool), ValueNodeType.Boolean },
// Unity types
{typeof(Vector3), ValueNodeType.Object },
};
static ValueNodeType ToJsonType(Type t)
{
ValueNodeType jsonValueType;
if (s_typeMap.TryGetValue(t, out jsonValueType))
{
return jsonValueType;
}
if (t.IsArray)
{
return ValueNodeType.Array;
}
if (t.GetIsGenericList())
{
return ValueNodeType.Array;
}
//if (t.IsClass)
{
return ValueNodeType.Object;
}
//throw new NotImplementedException(string.Format("No JsonType for {0}", t));
}
public static IJsonSchemaValidator Create(Type t, BaseJsonSchemaAttribute a, ItemJsonSchemaAttribute ia)
{
return Create(ToJsonType(t), t, a, ia);
}
}
}