Skip to content

Make JSONArray and JSONObject generic(#215) #259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion accessors-smart/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ limitations under the License.
</licenses>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.outputTimestamp>10</project.build.outputTimestamp>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
Expand Down
1 change: 0 additions & 1 deletion json-smart-action/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
</licenses>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.outputTimestamp>10</project.build.outputTimestamp>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>5.12.1</junit.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public ElementRemover(Map<String, Object> elementsToRemove) {
elementsToRemove == null ? Collections.<String, Object>emptyMap() : elementsToRemove;
}

public ElementRemover(JSONObject elementsToRemove) {
public ElementRemover(JSONObject<Object> elementsToRemove) {
this.elementsToRemove =
elementsToRemove == null ? Collections.<String, Object>emptyMap() : elementsToRemove;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class PathLocator {
protected List<String> pathsToFind;
protected PathDelimiter pathDelimiter = new DotDelimiter().withAcceptDelimiterInNodeName(false);

public PathLocator(JSONArray pathsToFind) {
public PathLocator(JSONArray<Object> pathsToFind) {
if (pathsToFind == null || pathsToFind.isEmpty()) {
this.pathsToFind = Collections.emptyList();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
public class PathRemover {
protected List<String> pathsToRemove;

public PathRemover(JSONArray pathsToRemove) {
public PathRemover(JSONArray<Object> pathsToRemove) {
if (pathsToRemove == null || pathsToRemove.isEmpty()) {
this.pathsToRemove = Collections.emptyList();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
public class PathReplicator {
protected List<String> pathsToCopy;

public PathReplicator(JSONArray pathsToCopy) {
public PathReplicator(JSONArray<Object> pathsToCopy) {
if (pathsToCopy == null || pathsToCopy.isEmpty()) {
this.pathsToCopy = Collections.emptyList();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class PathsRetainer {
protected List<String> pathsToRetain;
protected PathDelimiter pathDelimiter = new DotDelimiter().withAcceptDelimiterInNodeName(false);

public PathsRetainer(JSONArray pathsToRetain) {
public PathsRetainer(JSONArray<Object> pathsToRetain) {
if (pathsToRetain == null || pathsToRetain.isEmpty()) {
this.pathsToRetain = Collections.emptyList();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public class CopyPathsAction implements JSONNavigateAction {
protected Stack<Object> destNodeStack;

@Override
public boolean start(JSONObject source, Collection<String> pathsToCopy) {
if (source == null) {
public boolean start(JSONObject<Object> objectToNavigate, Collection<String> pathsToCopy) {
if (objectToNavigate == null) {
destTree = null;
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
* @author [email protected]
* @since 15 June 2016.
*/
public interface JSONNavigateAction extends NavigateAction<JSONObject, JSONArray> {}
public interface JSONNavigateAction extends NavigateAction<JSONObject<Object>, JSONArray<Object>> {}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author [email protected]
* @since 15 June 2016.
*/
public class JSONNavigator extends TreeNavigator<JSONObject, JSONArray> {
public class JSONNavigator extends TreeNavigator<JSONObject<Object>, JSONArray<Object>> {

public JSONNavigator(JSONNavigateAction action, List<String> pathsToNavigate) {
super(action, pathsToNavigate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
*
* @author [email protected]
*/
public interface JSONTraverseAction extends TreeTraverseAction<JSONObject, JSONArray> {}
public interface JSONTraverseAction extends TreeTraverseAction<JSONObject<Object>, JSONArray<Object>> {}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @author [email protected]
*/
public class JSONTraverser extends TreeTraverser<JSONObject, JSONArray> {
public class JSONTraverser extends TreeTraverser<JSONObject<Object>, JSONArray<Object>> {
public JSONTraverser(JSONTraverseAction action) {
super(action, new DotDelimiter());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private PathLocator switchKeyToRemove(Object keysToFind) {
return new PathLocator((String[]) null);
} else if (keysToFind == null && m % 4 == 2) {
// System.out.println("cast to JSONArray");
return new PathLocator((JSONArray) null);
return new PathLocator((JSONArray<Object>) null);
} else if (keysToFind == null && m % 4 == 3) {
// System.out.println("cast to List<String>");
return new PathLocator((List<String>) null);
Expand All @@ -150,7 +150,7 @@ private PathLocator switchKeyToRemove(Object keysToFind) {
} else if (keysToFind instanceof String[]) {
return new PathLocator((String[]) keysToFind);
} else if (keysToFind instanceof JSONArray) {
return new PathLocator((JSONArray) keysToFind);
return new PathLocator((JSONArray<Object>) keysToFind);
} else if (keysToFind instanceof List<?>) {
return new PathLocator((List<String>) keysToFind);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private PathRemover switchKeyToRemove(Object keyToRemove) {
return new PathRemover((String[]) null);
} else if (keyToRemove == null && m % 4 == 2) {
// System.out.println("cast to JSONArray");
return new PathRemover((JSONArray) null);
return new PathRemover((JSONArray<Object>) null);
} else if (keyToRemove == null && m % 4 == 3) {
// System.out.println("cast to List<String>");
return new PathRemover((List<String>) null);
Expand All @@ -138,7 +138,7 @@ private PathRemover switchKeyToRemove(Object keyToRemove) {
} else if (keyToRemove instanceof String[]) {
return new PathRemover((String[]) keyToRemove);
} else if (keyToRemove instanceof JSONArray) {
return new PathRemover((JSONArray) keyToRemove);
return new PathRemover((JSONArray<Object>) keyToRemove);
} else if (keyToRemove instanceof List<?>) {
return new PathRemover((List<String>) keyToRemove);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private PathReplicator switchKeyToCopy(Object pathsToCopy) {
return new PathReplicator((String[]) null);
} else if (pathsToCopy == null && m % 4 == 2) {
// System.out.println("cast to JSONArray");
return new PathReplicator((JSONArray) null);
return new PathReplicator((JSONArray<Object>) null);
} else if (pathsToCopy == null && m % 4 == 3) {
// System.out.println("cast to List<String>");
return new PathReplicator((List<String>) null);
Expand All @@ -193,7 +193,7 @@ private PathReplicator switchKeyToCopy(Object pathsToCopy) {
} else if (pathsToCopy instanceof String[]) {
return new PathReplicator((String[]) pathsToCopy);
} else if (pathsToCopy instanceof JSONArray) {
return new PathReplicator((JSONArray) pathsToCopy);
return new PathReplicator((JSONArray<Object>) pathsToCopy);
} else if (pathsToCopy instanceof List<?>) {
return new PathReplicator((List<String>) pathsToCopy);
} else {
Expand All @@ -212,7 +212,7 @@ private PathReplicator switchKeyToCopy2(Object pathsToCopy) {
return new PathReplicator((String[]) null);
} else if (pathsToCopy == null && m % 4 == 2) {
// System.out.println("cast to JSONArray");
return new PathReplicator((JSONArray) null);
return new PathReplicator((JSONArray<Object>) null);
} else if (pathsToCopy == null && m % 4 == 3) {
// System.out.println("cast to List<String>");
return new PathReplicator((List<String>) null);
Expand All @@ -221,7 +221,7 @@ private PathReplicator switchKeyToCopy2(Object pathsToCopy) {
} else if (pathsToCopy instanceof String[]) {
return new PathReplicator((String[]) pathsToCopy);
} else if (pathsToCopy instanceof JSONArray) {
return new PathReplicator((JSONArray) pathsToCopy);
return new PathReplicator((JSONArray<Object>) pathsToCopy);
} else if (pathsToCopy instanceof List<?>) {
return new PathReplicator((List<String>) pathsToCopy);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private PathsRetainer switchKeyToRemove(Object keyToKeep) {
return new PathsRetainer((String[]) null);
} else if (keyToKeep == null && m % 4 == 2) {
// System.out.println("cast to JSONArray");
return new PathsRetainer((JSONArray) null);
return new PathsRetainer((JSONArray<Object>) null);
} else if (keyToKeep == null && m % 4 == 3) {
// System.out.println("cast to List<String>");
return new PathsRetainer((List<String>) null);
Expand All @@ -164,7 +164,7 @@ private PathsRetainer switchKeyToRemove(Object keyToKeep) {
} else if (keyToKeep instanceof String[]) {
return new PathsRetainer((String[]) keyToKeep);
} else if (keyToKeep instanceof JSONArray) {
return new PathsRetainer((JSONArray) keyToKeep);
return new PathsRetainer((JSONArray<Object>) keyToKeep);
} else if (keyToKeep instanceof List<?>) {
return new PathsRetainer((List<String>) keyToKeep);
} else {
Expand Down
1 change: 0 additions & 1 deletion json-smart/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ limitations under the License.
</licenses>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.outputTimestamp>10</project.build.outputTimestamp>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>5.12.1</junit.version>
Expand Down
6 changes: 3 additions & 3 deletions json-smart/src/main/java/net/minidev/json/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
* @author FangYidong &lt;[email protected]&gt;
* @author Uriel Chemouni &lt;[email protected]&gt;
*/
public class JSONArray extends ArrayList<Object>
implements List<Object>, JSONAwareEx, JSONStreamAwareEx {
public class JSONArray<E> extends ArrayList<E>
implements List<E>, JSONAwareEx, JSONStreamAwareEx {
private static final long serialVersionUID = 9106884089231309568L;

public JSONArray() {}
Expand Down Expand Up @@ -87,7 +87,7 @@ public static void writeJSONString(List<? extends Object> list, Appendable out)
* @param element element to be appended to this array.
* @return this
*/
public JSONArray appendElement(Object element) {
public JSONArray appendElement(E element) {
add(element);
return this;
}
Expand Down
22 changes: 17 additions & 5 deletions json-smart/src/main/java/net/minidev/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @author FangYidong &lt;[email protected]&gt;
* @author Uriel Chemouni &lt;[email protected]&gt;
*/
public class JSONObject extends HashMap<String, Object> implements JSONAwareEx, JSONStreamAwareEx {
public class JSONObject<E> extends HashMap<String, E> implements JSONAwareEx, JSONStreamAwareEx {
private static final long serialVersionUID = -503443796854799292L;

public JSONObject() {
Expand Down Expand Up @@ -92,7 +92,7 @@ public static void writeJSONKV(String key, Object value, Appendable out, JSONSty
* @param fieldValue value to be associated with the specified key
* @return this
*/
public JSONObject appendField(String fieldName, Object fieldValue) {
public JSONObject appendField(String fieldName, E fieldValue) {
put(fieldName, fieldValue);
return this;
}
Expand Down Expand Up @@ -145,7 +145,7 @@ public Number getAsNumber(String key) {
* Allows creation of a JSONObject from a Map. After that, both the generated JSONObject and the
* Map can be modified independently.
*/
public JSONObject(Map<String, ?> map) {
public JSONObject(Map<String, ? extends E> map) {
super(map);
}

Expand Down Expand Up @@ -198,9 +198,21 @@ protected static JSONObject merge(JSONObject o1, Object o2, boolean overwrite) {
throw new RuntimeException("JSON merge can not merge JSONObject with " + o2.getClass());
}

static class A {
int a;

public int getA() {
return a;
}

public void setA(int a) {
this.a = a;
}
}

private static JSONObject merge(JSONObject o1, JSONObject o2, boolean overwrite) {
if (o2 == null) return o1;
for (String key : o1.keySet()) {
for (Object key : o1.keySet()) {
Object value1 = o1.get(key);
Object value2 = o2.get(key);
if (value2 == null) continue;
Expand All @@ -227,7 +239,7 @@ private static JSONObject merge(JSONObject o1, JSONObject o2, boolean overwrite)
+ " with "
+ value2.getClass().getName());
}
for (String key : o2.keySet()) {
for (Object key : o2.keySet()) {
if (o1.containsKey(key)) continue;
o1.put(key, o2.get(key));
}
Expand Down