forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSON.qll
More file actions
167 lines (145 loc) · 3.49 KB
/
Copy pathJSON.qll
File metadata and controls
167 lines (145 loc) · 3.49 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
/**
* Provides classes for working with JSON data.
*/
import javascript
/**
* A JSON-encoded value, which may be a primitive value, an array or an object.
*
* Examples:
*
* ```
* null
* true
* false
* 42
* "a string"
* [ 1, 2, 3 ]
* { "value": 0 }
* ```
*/
class JSONValue extends @json_value, Locatable {
override Location getLocation() { json_locations(this, result) }
/** Gets the parent value to which this value belongs, if any. */
JSONValue getParent() { json(this, _, result, _, _) }
/** Gets the `i`th child value of this value. */
JSONValue getChild(int i) { json(result, _, this, i, _) }
/** Holds if this JSON value is the top level element in its enclosing file. */
predicate isTopLevel() { not exists(getParent()) }
override string toString() { json(this, _, _, _, result) }
/** Gets the JSON file containing this value. */
File getJsonFile() {
exists(Location loc |
json_locations(this, loc) and
result = loc.getFile()
)
}
override string getAPrimaryQlClass() { result = "JSONValue" }
}
/**
* A JSON-encoded primitive value.
*
* Examples:
*
* ```
* null
* true
* false
* 42
* "a string"
* ```
*/
abstract class JSONPrimitiveValue extends JSONValue {
/** Gets a string representation of the encoded value. */
string getValue() { json_literals(result, _, this) }
/** Gets the source text of the encoded value; for strings, this includes quotes. */
string getRawValue() { json_literals(_, result, this) }
}
/**
* A JSON-encoded null value.
*
* Example:
*
* ```
* null
* ```
*/
class JSONNull extends @json_null, JSONPrimitiveValue {
override string getAPrimaryQlClass() { result = "JSONNull" }
}
/**
* A JSON-encoded Boolean value.
*
* Examples:
*
* ```
* true
* false
* ```
*/
class JSONBoolean extends @json_boolean, JSONPrimitiveValue {
override string getAPrimaryQlClass() { result = "JSONBoolean" }
}
/**
* A JSON-encoded number.
*
* Examples:
*
* ```
* 42
* 1.0
* ```
*/
class JSONNumber extends @json_number, JSONPrimitiveValue {
override string getAPrimaryQlClass() { result = "JSONNumber" }
}
/**
* A JSON-encoded string value.
*
* Example:
*
* ```
* "a string"
* ```
*/
class JSONString extends @json_string, JSONPrimitiveValue {
override string getAPrimaryQlClass() { result = "JSONString" }
}
/**
* A JSON-encoded array.
*
* Example:
*
* ```
* [ 1, 2, 3 ]
* ```
*/
class JSONArray extends @json_array, JSONValue {
/** Gets the value of the `i`th element of this array. */
JSONValue getElementValue(int i) { result = getChild(i) }
/** Gets the string value of the `i`th element of this array. */
string getElementStringValue(int i) { result = getElementValue(i).(JSONString).getValue() }
override string getAPrimaryQlClass() { result = "JSONArray" }
}
/**
* A JSON-encoded object.
*
* Example:
*
* ```
* { "value": 0 }
* ```
*/
class JSONObject extends @json_object, JSONValue {
/** Gets the value of property `name` of this object. */
JSONValue getPropValue(string name) { json_properties(this, name, result) }
/** Gets the string value of property `name` of this object. */
string getPropStringValue(string name) { result = getPropValue(name).(JSONString).getValue() }
override string getAPrimaryQlClass() { result = "JSONObject" }
}
/**
* An error reported by the JSON parser.
*/
class JSONParseError extends @json_parse_error, Error {
override Location getLocation() { json_locations(this, result) }
override string getMessage() { json_errors(this, result) }
}