JsonArray.java
/***************************************************************************
Copyright 2014 Emily Estes
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***************************************************************************/
package net.metanotion.json;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Iterator;
/** This class represents a list of JSON values. */
public final class JsonArray implements Iterable<Object>, Json {
private static final String INVALID_TYPE = "Invalid type to add to this JsonArray";
private final ArrayList<Object> arr = new ArrayList<>();
@Override public Iterator<Object> iterator() {
final Iterator<Object> it = arr.iterator();
return new Iterator<Object>() {
@Override public boolean hasNext() { return it.hasNext(); }
@Override public Object next() { return it.next(); }
@Override public void remove() { throw new UnsupportedOperationException(); }
};
}
/** Retrieve the element of the array at the index provided.
@param index The index of the array to retrieve.
@return The value stored at that index.
@throws IndexOutOfBoundsException if the index is not within the bounds of the list.
*/
public Object get(final int index) { return arr.get(index); }
/** Append the value to the end of the list.
@param val The value to append to the end of the list.
@throws IllegalArgumentException if the value is an inappropriate type.
*/
public void add(final Object val) {
if( (val == null)
|| (val instanceof JsonObject)
|| (val instanceof JsonArray)
|| (val instanceof Number)
|| (val instanceof Boolean)
|| (val instanceof String)) {
arr.add(val);
} else {
throw new IllegalArgumentException(INVALID_TYPE);
}
}
/** Set the value of the given index to the provided value.
@param index The index of the array to set.
@param val The value to store at the index.
@return The value previously at the index.
@throws IllegalArgumentException if the value is an inappropriate type.
@throws IndexOutOfBoundsException if the index is not within the bounds of the list.
*/
public Object put(final int index, final Object val) {
if( (val == null)
|| (val instanceof JsonObject)
|| (val instanceof JsonArray)
|| (val instanceof Number)
|| (val instanceof Boolean)
|| (val instanceof String)) {
return arr.set(index, val);
} else {
throw new IllegalArgumentException(INVALID_TYPE);
}
}
/** Remove the value at the given index and shift the array down one element.
@param index The index of the array to remove.
@return The value previously at the index.
@throws IndexOutOfBoundsException if the index is not within the bounds of the list.
*/
public Object remove(final int index) { return arr.remove(index); }
/** Return the length of the array.
@return The number of elements in the array.
*/
public int size() { return arr.size(); }
@Override public int hashCode() { return arr.hashCode(); }
@Override public boolean equals(final Object o) {
if(!(o instanceof JsonArray)) { return false; }
return ((JsonArray) o).arr.equals(arr);
}
/** Produce a JSON encoded string representing the values contained in this instance.
@return a JSON encoded string representing this instance.
*/
@Override public String toString() {
final StringWriter out = new StringWriter();
(new JsonWriter(out)).write(this);
return out.toString();
}
private static final StreamingParser parser = new StreamingParser();
/** Parse a JSON encoded string and produce an instance of JsonArray from it.
@param json The JSON encoded string to read.
@return An instance of JsonArray representing the data encoded in the string.
@throws RuntimeException if the string does not represent a JSON array.
@throws IOException if there is an I/O reading the string.
*/
public static JsonArray read(final String json) throws IOException { return read(new StringReader(json)); }
/** Parse a JSON encoded stream and produce an instance of JsonArray from it.
@param json The stream JSON encoded data to read.
@return An instance of JsonArray representing the data encoded in the string.
@throws RuntimeException if the stream does not encode a JSON array.
@throws IOException if there is an I/O reading the stream.
*/
public static JsonArray read(final Reader json) throws IOException {
return parser.parse(json, new JsonReader<JsonArray>());
}
}