Skip to content

Commit 9cd31af

Browse files
committed
fix bug where results accumulate if you reuse a jsonpath instance
1 parent a858885 commit 9cd31af

File tree

1 file changed

+19
-22
lines changed
  • src/main/kotlin/com/nfeld/jsonpathlite

1 file changed

+19
-22
lines changed

src/main/kotlin/com/nfeld/jsonpathlite/Token.kt

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ internal data class ArrayAccessorToken(val index: Int) : Token {
3131
* @param indices indices to access, can be negative which means to access from end
3232
*/
3333
internal data class MultiArrayAccessorToken(val indices: List<Int>) : Token {
34-
private val result = JSONArray()
35-
3634
override fun read(json: Any): Any? {
35+
val result = JSONArray()
36+
3737
if (json is JSONArray) {
3838
val jsonLength = json.length()
3939
indices.forEach { index ->
@@ -108,9 +108,9 @@ internal data class ObjectAccessorToken(val key: String) : Token {
108108
* @param keys keys to access for which key/values to return
109109
*/
110110
internal data class MultiObjectAccessorToken(val keys: List<String>) : Token {
111-
private val result = JSONObject()
112-
113111
override fun read(json: Any): Any? {
112+
val result = JSONObject()
113+
114114
return if (json is JSONObject) {
115115
keys.forEach { key ->
116116
json.opt(key)?.let {
@@ -128,9 +128,7 @@ internal data class MultiObjectAccessorToken(val keys: List<String>) : Token {
128128
* @param targetKeys keys to find values for
129129
*/
130130
internal data class DeepScanObjectAccessorToken(val targetKeys: List<String>) : Token {
131-
private val result = JSONArray()
132-
133-
private fun scan(jsonValue: Any) {
131+
private fun scan(jsonValue: Any, result: JSONArray) {
134132
when (jsonValue) {
135133
is JSONObject -> {
136134
// first add all values from keys requested to result
@@ -152,14 +150,14 @@ internal data class DeepScanObjectAccessorToken(val targetKeys: List<String>) :
152150
jsonValue.keySet().forEach { objKey ->
153151
val objValue = jsonValue.opt(objKey)
154152
if (objValue is JSONObject || objValue is JSONArray) {
155-
scan(objValue)
153+
scan(objValue, result)
156154
}
157155
}
158156
}
159157
is JSONArray -> {
160158
jsonValue.forEach {
161159
if (it is JSONObject || it is JSONArray) {
162-
scan(it)
160+
scan(it, result)
163161
}
164162
}
165163
}
@@ -168,7 +166,8 @@ internal data class DeepScanObjectAccessorToken(val targetKeys: List<String>) :
168166
}
169167

170168
override fun read(json: Any): Any? {
171-
scan(json)
169+
val result = JSONArray()
170+
scan(json, result)
172171
return result
173172
}
174173
}
@@ -179,16 +178,14 @@ internal data class DeepScanObjectAccessorToken(val targetKeys: List<String>) :
179178
* @param indices indices to retrieve values/objects for
180179
*/
181180
internal data class DeepScanArrayAccessorToken(val indices: List<Int>) : Token {
182-
private val result = JSONArray()
183-
184-
private fun scan(jsonValue: Any) {
181+
private fun scan(jsonValue: Any, result: JSONArray) {
185182
when (jsonValue) {
186183
is JSONObject -> {
187184
// traverse all key/value pairs and recursively scan underlying objects/arrays
188185
jsonValue.keySet().forEach { objKey ->
189186
val objValue = jsonValue.opt(objKey)
190187
if (objValue is JSONObject || objValue is JSONArray) {
191-
scan(objValue)
188+
scan(objValue, result)
192189
}
193190
}
194191
}
@@ -201,7 +198,7 @@ internal data class DeepScanArrayAccessorToken(val indices: List<Int>) : Token {
201198
// now recursively scan underlying objects/arrays
202199
jsonValue.forEach {
203200
if (it is JSONObject || it is JSONArray) {
204-
scan(it)
201+
scan(it, result)
205202
}
206203
}
207204
}
@@ -210,7 +207,8 @@ internal data class DeepScanArrayAccessorToken(val indices: List<Int>) : Token {
210207
}
211208

212209
override fun read(json: Any): Any? {
213-
scan(json)
210+
val result = JSONArray()
211+
scan(json, result)
214212
return result
215213
}
216214
}
@@ -227,16 +225,14 @@ internal data class DeepScanArrayAccessorToken(val indices: List<Int>) : Token {
227225
internal data class DeepScanLengthBasedArrayAccessorToken(val startIndex: Int,
228226
val endIndex: Int? = null,
229227
val offsetFromEnd: Int = 0) : Token {
230-
private val result = JSONArray()
231-
232-
private fun scan(jsonValue: Any) {
228+
private fun scan(jsonValue: Any, result: JSONArray) {
233229
when (jsonValue) {
234230
is JSONObject -> {
235231
// traverse all key/value pairs and recursively scan underlying objects/arrays
236232
jsonValue.keySet().forEach { objKey ->
237233
val objValue = jsonValue.opt(objKey)
238234
if (objValue is JSONObject || objValue is JSONArray) {
239-
scan(objValue)
235+
scan(objValue, result)
240236
}
241237
}
242238
}
@@ -252,7 +248,7 @@ internal data class DeepScanLengthBasedArrayAccessorToken(val startIndex: Int,
252248
// now recursively scan underlying objects/arrays
253249
jsonValue.forEach {
254250
if (it is JSONObject || it is JSONArray) {
255-
scan(it)
251+
scan(it, result)
256252
}
257253
}
258254
}
@@ -261,7 +257,8 @@ internal data class DeepScanLengthBasedArrayAccessorToken(val startIndex: Int,
261257
}
262258

263259
override fun read(json: Any): Any? {
264-
scan(json)
260+
val result = JSONArray()
261+
scan(json, result)
265262
return result
266263
}
267264
}

0 commit comments

Comments
 (0)