X Tutup
package javaxt.json; import javaxt.json.JSONObject.JSONTokener; import java.io.IOException; import java.io.Writer; import javaxt.utils.Value; //****************************************************************************** //** JSONArray //****************************************************************************** /** * A JSON array is simply an array of objects. The string representation of a * JSON array is a widely-used standard format for exchanging data. The string * begins with a left square bracket "[" and ends with a right square bracket * "]". Each object in the array is separated by comma ",". * * @author Source adapted from json.org (2016-08-15) * ******************************************************************************/ public class JSONArray implements Iterable { private final java.util.ArrayList arr; //************************************************************************** //** Constructor //************************************************************************** /** Used to create a new/empty array. */ public JSONArray() { arr = new java.util.ArrayList(); } //************************************************************************** //** Constructor //************************************************************************** /** Used to create a new array from a String (e.g. "[1,2,3]"). */ public JSONArray(String source) throws JSONException { this(new JSONTokener(source)); } //************************************************************************** //** Constructor //************************************************************************** protected JSONArray(JSONTokener x) throws JSONException { this(); if (x.nextClean() != '[') { throw x.syntaxError("A JSONArray text must start with '['"); } char nextChar = x.nextClean(); if (nextChar == 0) { // array is unclosed. No ']' found, instead EOF throw x.syntaxError("Expected a ',' or ']'"); } if (nextChar != ']') { x.back(); for (;;) { if (x.nextClean() == ',') { x.back(); //arr.add(JSONObject.NULL); } else { x.back(); add(x.nextValue()); } switch (x.nextClean()) { case 0: // array is unclosed. No ']' found, instead EOF throw x.syntaxError("Expected a ',' or ']'"); case ',': nextChar = x.nextClean(); if (nextChar == 0) { // array is unclosed. No ']' found, instead EOF throw x.syntaxError("Expected a ',' or ']'"); } if (nextChar == ']') { return; } x.back(); break; case ']': return; default: throw x.syntaxError("Expected a ',' or ']'"); } } } } //************************************************************************** //** iterator //************************************************************************** @Override public java.util.Iterator iterator() { return arr.iterator(); } //************************************************************************** //** length //************************************************************************** /** Returns the number of elements in the JSONArray, included nulls. */ public int length() { return arr.size(); } //************************************************************************** //** isEmpty //************************************************************************** /** Returns true if there are no entries in the array. */ public boolean isEmpty(){ return arr.isEmpty(); } //************************************************************************** //** get //************************************************************************** /** Returns the object value associated with an index. */ public JSONValue get(int index) { return (index < 0 || index >= this.length()) ? new JSONValue(null) : arr.get(index); } //************************************************************************** //** add //************************************************************************** /** Appends an object to the array. */ public void add(Object object) throws JSONException { JSONValue v; Object o; if (object instanceof JSONValue){ v = (JSONValue) object; o = v.toObject(); } else if (object instanceof Value){ o = ((Value) object).toObject(); v = new JSONValue(object); } else{ o = object; v = new JSONValue(object); } JSONObject.testValidity(o); arr.add(v); } //************************************************************************** //** set //************************************************************************** /** Updates an object to the array. Returns the original value that was * associated with the index. */ public JSONValue set(int index, Object object){ JSONValue v; Object o; if (object instanceof JSONValue){ v = (JSONValue) object; o = v.toObject(); } else if (object instanceof Value){ o = ((Value) object).toObject(); v = new JSONValue(object); } else{ o = object; v = new JSONValue(object); } JSONObject.testValidity(o); Object obj = arr.set(index, v); return new JSONValue(obj); } //************************************************************************** //** remove //************************************************************************** /** Remove entry. Returns the value that was associated with the index. */ public JSONValue remove(int index) { return index >= 0 && index < this.length() ? new JSONValue(arr.remove(index)) : new JSONValue(null); } //************************************************************************** //** equals //************************************************************************** /** Returns true if the given object is a JSONArray and the JSONArray * contains the same entries as this array. Order is important. */ public boolean equals(Object obj){ if (obj instanceof JSONArray){ JSONArray arr = (JSONArray) obj; if (arr.length()==this.length()){ for (int i=0; i 0) { writer.write('\n'); } JSONObject.indent(writer, newindent); try { JSONObject.writeValue(writer, arr.get(i).toObject(), indentFactor, newindent); } catch (Exception e) { throw new JSONException("Unable to write JSONArray value at index: " + i, e); } commanate = true; } if (indentFactor > 0) { writer.write('\n'); } JSONObject.indent(writer, indent); } writer.write(']'); return writer; } catch (IOException e) { throw new JSONException(e); } } }
X Tutup