From b079eb81149265edba0a1b50f38fbb6b004bee3e Mon Sep 17 00:00:00 2001 From: 33447142+ZachsCoffee <33447142+ZachsCoffee@users.noreply.github.com> Date: Sun, 8 Oct 2023 17:30:02 +0300 Subject: [PATCH] Make the JSONArray class to implement the java.util.List interface so it will support the streams but also all other functionality of a list --- src/main/java/org/json/JSONArray.java | 98 +++++++++++++++++++++++++- src/main/java/org/json/JSONObject.java | 6 ++ 2 files changed, 103 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index b0c912d1f..0729d7d10 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -14,6 +14,7 @@ import java.util.Collection; import java.util.Iterator; import java.util.List; +import java.util.ListIterator; import java.util.Map; @@ -61,7 +62,7 @@ * @author JSON.org * @version 2016-08/15 */ -public class JSONArray implements Iterable { +public class JSONArray implements List { /** * The arrayList where the JSONArray's properties are kept. @@ -229,6 +230,54 @@ public Iterator iterator() { return this.myArrayList.iterator(); } + @Override + public Object[] toArray() { + return myArrayList.toArray(); + } + + @Override + public T[] toArray(T[] array) { + return myArrayList.toArray(array); + } + + @Override + public boolean add(Object object) { + JSONObject.testValidity(object); + return myArrayList.add(object); + } + + @Override + public boolean remove(Object object) { + return myArrayList.remove(object); + } + + @Override + public boolean containsAll(Collection collection) { + return myArrayList.containsAll(collection); + } + + @Override + public boolean addAll(Collection collection) { + JSONObject.testValidity(collection); + return myArrayList.addAll(collection); + } + + @Override + public boolean addAll(int index, Collection collection) { + JSONObject.testValidity(collection); + return myArrayList.addAll(index, collection); + } + + @Override + public boolean removeAll(Collection collection) { + return myArrayList.removeAll(collection); + } + + @Override + public boolean retainAll(Collection collection) { + return myArrayList.retainAll(collection); + } + /** * Get the object value associated with an index. * @@ -246,6 +295,18 @@ public Object get(int index) throws JSONException { return object; } + @Override + public Object set(int index, Object object) { + JSONObject.testValidity(object); + return myArrayList.set(index, object); + } + + @Override + public void add(int index, Object object) { + JSONObject.testValidity(object); + myArrayList.add(index, object); + } + /** * Get the boolean value associated with an index. The string values "true" * and "false" are converted to boolean. @@ -1525,6 +1586,31 @@ public Object remove(int index) { : null; } + @Override + public int indexOf(Object object) { + return myArrayList.indexOf(object); + } + + @Override + public int lastIndexOf(Object object) { + return myArrayList.lastIndexOf(object); + } + + @Override + public ListIterator listIterator() { + return myArrayList.listIterator(); + } + + @Override + public ListIterator listIterator(int index) { + return myArrayList.listIterator(index); + } + + @Override + public List subList(int from, int to) { + return myArrayList.subList(from, to); + } + /** * Determine if two JSONArrays are similar. * They must contain similar sequences. @@ -1762,6 +1848,11 @@ public List toList() { return results; } + @Override + public int size() { + return myArrayList.size(); + } + /** * Check if JSONArray is empty. * @@ -1771,6 +1862,11 @@ public boolean isEmpty() { return this.myArrayList.isEmpty(); } + @Override + public boolean contains(Object object) { + return myArrayList.contains(object); + } + /** * Add a collection's elements to the JSONArray. * diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index acef67d9f..71dbc0554 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -2487,6 +2487,12 @@ public static Object stringToValue(String string) { return string; } + public static > void testValidity(T objects) { + for (Object object : objects) { + testValidity(object); + } + } + /** * Throw an exception if the object is a NaN or infinite number. *