forked from Bunny83/SimpleJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleJSONUnity.cs
462 lines (447 loc) · 15.7 KB
/
SimpleJSONUnity.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#if UNITY_5_3_OR_NEWER
#region License and information
/* * * * *
*
* Unity extension for the SimpleJSON framework. It does only work together with
* the SimpleJSON.cs
* It provides several helpers and conversion operators to serialize/deserialize
* common Unity types such as Vector2/3/4, Rect, RectOffset, Quaternion and
* Matrix4x4 as JSONObject or JSONArray.
* This extension will add 3 static settings to the JSONNode class:
* ( VectorContainerType, QuaternionContainerType, RectContainerType ) which
* control what node type should be used for serializing the given type. So a
* Vector3 as array would look like [12,32,24] and {"x":12, "y":32, "z":24} as
* object.
*
*
* The MIT License (MIT)
*
* Copyright (c) 2012-2017 Markus Göbel (Bunny83)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* * * * */
#endregion License and information
using UnityEngine;
namespace SimpleJSON
{
public enum JSONContainerType { Array, Object }
public partial class JSONNode
{
public static byte Color32DefaultAlpha = 255;
public static float ColorDefaultAlpha = 1f;
public static JSONContainerType VectorContainerType = JSONContainerType.Array;
public static JSONContainerType QuaternionContainerType = JSONContainerType.Array;
public static JSONContainerType RectContainerType = JSONContainerType.Array;
public static JSONContainerType ColorContainerType = JSONContainerType.Array;
private static JSONNode GetContainer(JSONContainerType aType)
{
if (aType == JSONContainerType.Array)
return new JSONArray();
return new JSONObject();
}
#region implicit conversion operators
public static implicit operator JSONNode(Vector2 aVec)
{
JSONNode n = GetContainer(VectorContainerType);
n.WriteVector2(aVec);
return n;
}
public static implicit operator JSONNode(Vector3 aVec)
{
JSONNode n = GetContainer(VectorContainerType);
n.WriteVector3(aVec);
return n;
}
public static implicit operator JSONNode(Vector4 aVec)
{
JSONNode n = GetContainer(VectorContainerType);
n.WriteVector4(aVec);
return n;
}
public static implicit operator JSONNode(Color aCol)
{
JSONNode n = GetContainer(ColorContainerType);
n.WriteColor(aCol);
return n;
}
public static implicit operator JSONNode(Color32 aCol)
{
JSONNode n = GetContainer(ColorContainerType);
n.WriteColor32(aCol);
return n;
}
public static implicit operator JSONNode(Quaternion aRot)
{
JSONNode n = GetContainer(QuaternionContainerType);
n.WriteQuaternion(aRot);
return n;
}
public static implicit operator JSONNode(Rect aRect)
{
JSONNode n = GetContainer(RectContainerType);
n.WriteRect(aRect);
return n;
}
public static implicit operator JSONNode(RectOffset aRect)
{
JSONNode n = GetContainer(RectContainerType);
n.WriteRectOffset(aRect);
return n;
}
public static implicit operator Vector2(JSONNode aNode)
{
return aNode.ReadVector2();
}
public static implicit operator Vector3(JSONNode aNode)
{
return aNode.ReadVector3();
}
public static implicit operator Vector4(JSONNode aNode)
{
return aNode.ReadVector4();
}
public static implicit operator Color(JSONNode aNode)
{
return aNode.ReadColor();
}
public static implicit operator Color32(JSONNode aNode)
{
return aNode.ReadColor32();
}
public static implicit operator Quaternion(JSONNode aNode)
{
return aNode.ReadQuaternion();
}
public static implicit operator Rect(JSONNode aNode)
{
return aNode.ReadRect();
}
public static implicit operator RectOffset(JSONNode aNode)
{
return aNode.ReadRectOffset();
}
#endregion implicit conversion operators
#region Vector2
public Vector2 ReadVector2(Vector2 aDefault)
{
if (IsObject)
return new Vector2(this["x"].AsFloat, this["y"].AsFloat);
if (IsArray)
return new Vector2(this[0].AsFloat, this[1].AsFloat);
return aDefault;
}
public Vector2 ReadVector2(string aXName, string aYName)
{
if (IsObject)
{
return new Vector2(this[aXName].AsFloat, this[aYName].AsFloat);
}
return Vector2.zero;
}
public Vector2 ReadVector2()
{
return ReadVector2(Vector2.zero);
}
public JSONNode WriteVector2(Vector2 aVec, string aXName = "x", string aYName = "y")
{
if (IsObject)
{
Inline = true;
this[aXName].AsFloat = aVec.x;
this[aYName].AsFloat = aVec.y;
}
else if (IsArray)
{
Inline = true;
this[0].AsFloat = aVec.x;
this[1].AsFloat = aVec.y;
}
return this;
}
#endregion Vector2
#region Vector3
public Vector3 ReadVector3(Vector3 aDefault)
{
if (IsObject)
return new Vector3(this["x"].AsFloat, this["y"].AsFloat, this["z"].AsFloat);
if (IsArray)
return new Vector3(this[0].AsFloat, this[1].AsFloat, this[2].AsFloat);
return aDefault;
}
public Vector3 ReadVector3(string aXName, string aYName, string aZName)
{
if (IsObject)
return new Vector3(this[aXName].AsFloat, this[aYName].AsFloat, this[aZName].AsFloat);
return Vector3.zero;
}
public Vector3 ReadVector3()
{
return ReadVector3(Vector3.zero);
}
public JSONNode WriteVector3(Vector3 aVec, string aXName = "x", string aYName = "y", string aZName = "z")
{
if (IsObject)
{
Inline = true;
this[aXName].AsFloat = aVec.x;
this[aYName].AsFloat = aVec.y;
this[aZName].AsFloat = aVec.z;
}
else if (IsArray)
{
Inline = true;
this[0].AsFloat = aVec.x;
this[1].AsFloat = aVec.y;
this[2].AsFloat = aVec.z;
}
return this;
}
#endregion Vector3
#region Vector4
public Vector4 ReadVector4(Vector4 aDefault)
{
if (IsObject)
return new Vector4(this["x"].AsFloat, this["y"].AsFloat, this["z"].AsFloat, this["w"].AsFloat);
if (IsArray)
return new Vector4(this[0].AsFloat, this[1].AsFloat, this[2].AsFloat, this[3].AsFloat);
return aDefault;
}
public Vector4 ReadVector4()
{
return ReadVector4(Vector4.zero);
}
public JSONNode WriteVector4(Vector4 aVec)
{
if (IsObject)
{
Inline = true;
this["x"].AsFloat = aVec.x;
this["y"].AsFloat = aVec.y;
this["z"].AsFloat = aVec.z;
this["w"].AsFloat = aVec.w;
}
else if (IsArray)
{
Inline = true;
this[0].AsFloat = aVec.x;
this[1].AsFloat = aVec.y;
this[2].AsFloat = aVec.z;
this[3].AsFloat = aVec.w;
}
return this;
}
#endregion Vector4
#region Color / Color32
public Color ReadColor(Color aDefault)
{
if (IsObject)
return new Color(this["r"].AsFloat, this["g"].AsFloat, this["b"].AsFloat, HasKey("a")?this["a"].AsFloat:ColorDefaultAlpha);
if (IsArray)
return new Color(this[0].AsFloat, this[1].AsFloat, this[2].AsFloat, (Count>3)?this[3].AsFloat:ColorDefaultAlpha);
return aDefault;
}
public Color ReadColor()
{
return ReadColor(Color.clear);
}
public JSONNode WriteColor(Color aCol)
{
if (IsObject)
{
Inline = true;
this["r"].AsFloat = aCol.r;
this["g"].AsFloat = aCol.g;
this["b"].AsFloat = aCol.b;
this["a"].AsFloat = aCol.a;
}
else if (IsArray)
{
Inline = true;
this[0].AsFloat = aCol.r;
this[1].AsFloat = aCol.g;
this[2].AsFloat = aCol.b;
this[3].AsFloat = aCol.a;
}
return this;
}
public Color32 ReadColor32(Color32 aDefault)
{
if (IsObject)
return new Color32((byte)this["r"].AsInt, (byte)this["g"].AsInt, (byte)this["b"].AsInt, (byte)(HasKey("a")?this["a"].AsInt:Color32DefaultAlpha));
if (IsArray)
return new Color32((byte)this[0].AsInt, (byte)this[1].AsInt, (byte)this[2].AsInt, (byte)((Count>3)?this[3].AsInt:Color32DefaultAlpha));
return aDefault;
}
public Color32 ReadColor32()
{
return ReadColor32(new Color32());
}
public JSONNode WriteColor32(Color32 aCol)
{
if (IsObject)
{
Inline = true;
this["r"].AsInt = aCol.r;
this["g"].AsInt = aCol.g;
this["b"].AsInt = aCol.b;
this["a"].AsInt = aCol.a;
}
else if (IsArray)
{
Inline = true;
this[0].AsInt = aCol.r;
this[1].AsInt = aCol.g;
this[2].AsInt = aCol.b;
this[3].AsInt = aCol.a;
}
return this;
}
#endregion Color / Color32
#region Quaternion
public Quaternion ReadQuaternion(Quaternion aDefault)
{
if (IsObject)
return new Quaternion(this["x"].AsFloat, this["y"].AsFloat, this["z"].AsFloat, this["w"].AsFloat);
if (IsArray)
return new Quaternion(this[0].AsFloat, this[1].AsFloat, this[2].AsFloat, this[3].AsFloat);
return aDefault;
}
public Quaternion ReadQuaternion()
{
return ReadQuaternion(Quaternion.identity);
}
public JSONNode WriteQuaternion(Quaternion aRot)
{
if (IsObject)
{
Inline = true;
this["x"].AsFloat = aRot.x;
this["y"].AsFloat = aRot.y;
this["z"].AsFloat = aRot.z;
this["w"].AsFloat = aRot.w;
}
else if (IsArray)
{
Inline = true;
this[0].AsFloat = aRot.x;
this[1].AsFloat = aRot.y;
this[2].AsFloat = aRot.z;
this[3].AsFloat = aRot.w;
}
return this;
}
#endregion Quaternion
#region Rect
public Rect ReadRect(Rect aDefault)
{
if (IsObject)
return new Rect(this["x"].AsFloat, this["y"].AsFloat, this["width"].AsFloat, this["height"].AsFloat);
if (IsArray)
return new Rect(this[0].AsFloat, this[1].AsFloat, this[2].AsFloat, this[3].AsFloat);
return aDefault;
}
public Rect ReadRect()
{
return ReadRect(new Rect());
}
public JSONNode WriteRect(Rect aRect)
{
if (IsObject)
{
Inline = true;
this["x"].AsFloat = aRect.x;
this["y"].AsFloat = aRect.y;
this["width"].AsFloat = aRect.width;
this["height"].AsFloat = aRect.height;
}
else if (IsArray)
{
Inline = true;
this[0].AsFloat = aRect.x;
this[1].AsFloat = aRect.y;
this[2].AsFloat = aRect.width;
this[3].AsFloat = aRect.height;
}
return this;
}
#endregion Rect
#region RectOffset
public RectOffset ReadRectOffset(RectOffset aDefault)
{
if (this is JSONObject)
return new RectOffset(this["left"].AsInt, this["right"].AsInt, this["top"].AsInt, this["bottom"].AsInt);
if (this is JSONArray)
return new RectOffset(this[0].AsInt, this[1].AsInt, this[2].AsInt, this[3].AsInt);
return aDefault;
}
public RectOffset ReadRectOffset()
{
return ReadRectOffset(new RectOffset());
}
public JSONNode WriteRectOffset(RectOffset aRect)
{
if (IsObject)
{
Inline = true;
this["left"].AsInt = aRect.left;
this["right"].AsInt = aRect.right;
this["top"].AsInt = aRect.top;
this["bottom"].AsInt = aRect.bottom;
}
else if (IsArray)
{
Inline = true;
this[0].AsInt = aRect.left;
this[1].AsInt = aRect.right;
this[2].AsInt = aRect.top;
this[3].AsInt = aRect.bottom;
}
return this;
}
#endregion RectOffset
#region Matrix4x4
public Matrix4x4 ReadMatrix()
{
Matrix4x4 result = Matrix4x4.identity;
if (IsArray)
{
for (int i = 0; i < 16; i++)
{
result[i] = this[i].AsFloat;
}
}
return result;
}
public JSONNode WriteMatrix(Matrix4x4 aMatrix)
{
if (IsArray)
{
Inline = true;
for (int i = 0; i < 16; i++)
{
this[i].AsFloat = aMatrix[i];
}
}
return this;
}
#endregion Matrix4x4
}
}
#endif