Skip to content

Commit 45bd72c

Browse files
committed
added JSONObject#query() and JSONPointer#query() methods
1 parent 792c6f6 commit 45bd72c

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

JSONArray.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ of this software and associated documentation files (the "Software"), to deal
3030
import java.lang.reflect.Array;
3131
import java.math.BigDecimal;
3232
import java.math.BigInteger;
33-
import java.util.*;
33+
import java.util.ArrayList;
34+
import java.util.Collection;
35+
import java.util.Iterator;
36+
import java.util.List;
37+
import java.util.Map;
3438

3539
/**
3640
* A JSONArray is an ordered sequence of values. Its external text form is a
@@ -955,6 +959,10 @@ public JSONArray put(int index, Object value) throws JSONException {
955959
}
956960
return this;
957961
}
962+
963+
public Object query(String jsonPointer) {
964+
return new JSONPointer(jsonPointer).queryFrom(this);
965+
}
958966

959967
/**
960968
* Remove an index and close the hole.

JSONObject.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,15 @@ of this software and associated documentation files (the "Software"), to deal
3232
import java.lang.reflect.Modifier;
3333
import java.math.BigDecimal;
3434
import java.math.BigInteger;
35-
import java.util.*;
35+
import java.util.Collection;
36+
import java.util.Enumeration;
37+
import java.util.HashMap;
38+
import java.util.Iterator;
39+
import java.util.Locale;
40+
import java.util.Map;
3641
import java.util.Map.Entry;
42+
import java.util.ResourceBundle;
43+
import java.util.Set;
3744

3845
/**
3946
* A JSONObject is an unordered collection of name/value pairs. Its external
@@ -1330,6 +1337,10 @@ public JSONObject putOpt(String key, Object value) throws JSONException {
13301337
}
13311338
return this;
13321339
}
1340+
1341+
public Object query(String jsonPointer) {
1342+
return new JSONPointer(jsonPointer).queryFrom(this);
1343+
}
13331344

13341345
/**
13351346
* Produce a string in double quotes with backslash sequences in all the

JSONPointer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ private String unescape(String token) {
4444
.replace("\\\\", "\\");
4545
}
4646

47-
public Object queryFrom(JSONObject document) {
47+
public Object queryFrom(Object document) {
4848
if (refTokens.isEmpty()) {
4949
return document;
5050
}

0 commit comments

Comments
 (0)