Skip to content

Commit 7c13163

Browse files
committed
JS: Lift JSON accessors to JSONValue
1 parent e1d0bbb commit 7c13163

File tree

1 file changed

+19
-10
lines changed
  • javascript/ql/src/semmle/javascript

1 file changed

+19
-10
lines changed

javascript/ql/src/semmle/javascript/JSON.qll

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ class JSONValue extends @json_value, Locatable {
4141
)
4242
}
4343

44+
/** If this is an object, gets the value of property `name`. */
45+
JSONValue getPropValue(string name) { json_properties(this, name, result) }
46+
47+
/** If this is an array, gets the value of the `i`th element. */
48+
JSONValue getElementValue(int i) { result = this.(JSONArray).getChild(i) }
49+
50+
/** If this is a string constant, gets the value of the string. */
51+
string getStringValue() { result = this.(JSONString).getValue() }
52+
53+
/** If this is an integer constant, gets its numeric value. */
54+
int getIntValue() { result = this.(JSONNumber).getValue().toInt() }
55+
56+
/** If this is a boolean constant, gets its boolean value. */
57+
boolean getBooleanValue() { result.toString() = this.(JSONBoolean).getValue() }
58+
4459
override string getAPrimaryQlClass() { result = "JSONValue" }
4560
}
4661

@@ -129,13 +144,10 @@ class JSONString extends @json_string, JSONPrimitiveValue {
129144
* ```
130145
*/
131146
class JSONArray extends @json_array, JSONValue {
132-
/** Gets the value of the `i`th element of this array. */
133-
JSONValue getElementValue(int i) { result = getChild(i) }
147+
override string getAPrimaryQlClass() { result = "JSONArray" }
134148

135149
/** Gets the string value of the `i`th element of this array. */
136-
string getElementStringValue(int i) { result = getElementValue(i).(JSONString).getValue() }
137-
138-
override string getAPrimaryQlClass() { result = "JSONArray" }
150+
string getElementStringValue(int i) { result = getElementValue(i).getStringValue() }
139151
}
140152

141153
/**
@@ -148,13 +160,10 @@ class JSONArray extends @json_array, JSONValue {
148160
* ```
149161
*/
150162
class JSONObject extends @json_object, JSONValue {
151-
/** Gets the value of property `name` of this object. */
152-
JSONValue getPropValue(string name) { json_properties(this, name, result) }
163+
override string getAPrimaryQlClass() { result = "JSONObject" }
153164

154165
/** Gets the string value of property `name` of this object. */
155-
string getPropStringValue(string name) { result = getPropValue(name).(JSONString).getValue() }
156-
157-
override string getAPrimaryQlClass() { result = "JSONObject" }
166+
string getPropStringValue(string name) { result = getPropValue(name).getStringValue() }
158167
}
159168

160169
/**

0 commit comments

Comments
 (0)