-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONParser.cs
More file actions
144 lines (138 loc) · 5.15 KB
/
JSONParser.cs
File metadata and controls
144 lines (138 loc) · 5.15 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
#if UNITY_2 || UNITY_3 || UNITY_4 || UNITY_5 || UNITY_6 || UNITY_7
using UnityEngine;
using Debug = UnityEngine.Debug;
#endif
using System;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
/**
* Recursively parses JSON files or strings.
*
* DOUBLE QUOTES ONLY. Escape as necessary. No char objects.
*
* JSON can otherwise be slightly invalid, but results could be unreliable if so (such as elements after error not being parsed, etc).
* Keep in mind that in JSON anything in double quotes are strings, so only primitives outside quotes are converted to correct type.
*
* All objects are converted to Dictionary{string, JSObject}, all arrays are converted to List{JSObject}, and all doubles are converted to floats
*
* The default debugger (Unity or System.Diagnostics) is invoked on error when debugging, but no exceptions are intentionally thrown at run time.
*
* Implemented using the 'JSONObject' library and my 'JSObject' library (both included in project)
*
* Usage: Standard square bracket syntax for dictionary, lists.
* JSObject c = parse(JSONString);
* c["character_name"]; //Objects access using field name
* c["expressions_list"]["exp_default"]; //Read JSON file to see format.
* c["expressions_list"]["exp_angry"][0];
* c["expressions_list"]["exp_confused"][0]["hi"]; //Arrays(Lists) access with number indices
*
* c.ContainsKey("character_name"); //Some methods and properties available directly
* c.Keys;
* ...
*
* Dictionary{string, JSObject} dict = c; //Assign to actual type for other methods and uses (foreach uses c.GetEnumerator())
* foreach (JSObject d in dict){ ... }
*/
public class JSONParser
{
/**
* Recursively parses JSON in given file, returning a JSObject object containing Dictionary{string, JSObject}, List{JSObject}, string, int, float, bool and null objects.
*
* @param filePath
* @returnA JSObject object representing the JSON data.
*/
public static JSObject parseFile(string filePath)
{
try
{
return parse(File.ReadAllText(filePath));
}
catch (Exception)
{
#if UNITY_2 || UNITY_3 || UNITY_4 || UNITY_5 || UNITY_6 || UNITY_7
Debug.LogWarning
#else
Debug.WriteLine
#endif
("Unable to read file (is filepath correct?).");
}
return null;
}
/**
* Recursively parses given JSONString, returning a JSObject object containing Dictionary{string, JSObject}, List{JSObject}, string, int, float, bool and null objects.
*
* @param JSONString
* @returnA JSObject object representing the JSON data.
*/
public static JSObject parse(string JSONString)
{
try
{
var c = new JSONObject(JSONString);
return parse(new JSONObject(JSONString));
}
catch (Exception)
{
#if UNITY_2 || UNITY_3 || UNITY_4 || UNITY_5 || UNITY_6 || UNITY_7
Debug.LogWarning
#else
Debug.WriteLine
#endif
("JSON parse error.");
}
return null;
}
/**
* Recursively parses given JSONObject, returning a JSObject object containing Dictionary{string, JSObject}, List{JSObject}, string, int, float, bool and null objects.
*
* @param obj
* @returnA JSObject object representing the JSON data.
*/
public static JSObject parse(JSONObject obj)
{
try
{
switch (obj.type)
{
case JSONObject.Type.OBJECT:
Dictionary<string, JSObject> d = new Dictionary<string, JSObject>();
for (int i = 0; i < obj.list.Count; i++)
{
d.Add(obj.keys[i], parse(obj.list[i]));
}
return d;
case JSONObject.Type.ARRAY:
List<JSObject> l = new List<JSObject>();
foreach (JSONObject j in obj.list)
{
l.Add(parse(j));
}
return l;
case JSONObject.Type.STRING:
return obj.str;
case JSONObject.Type.NUMBER:
if (obj.n % 1 == 0)
{
return (int)obj.n;
}
return obj.n;
case JSONObject.Type.BOOL:
return obj.b;
case JSONObject.Type.NULL:
default:
return null;
}
}
catch (Exception)
{
#if UNITY_2 || UNITY_3 || UNITY_4 || UNITY_5 || UNITY_6 || UNITY_7
Debug.LogWarning
#else
Debug.WriteLine
#endif
("JSON parse error.");
}
return null;
}
}