* *param array An array instance to populate instead of creating a new one.
*
* @throws JSONException Thrown if a parse error occurs, such as a malformed JSON array.
*/
public JSONArray parseArray(boolean ordered, JSONArray array) throws JSONException {
JSONArray result = null;
if(array != null){
result = array;
} else {
result = new JSONArray();
}
try {
if (lastToken != Token.TokenBrackL) throw new JSONException("Expecting '[' " + tokenizer.onLineCol());
lastToken = tokenizer.next();
while (true) {
if (lastToken == Token.TokenEOF) throw new JSONException("Unterminated array " + tokenizer.onLineCol());
/**
* End of the array.
*/
if (lastToken == Token.TokenBrackR) {
lastToken = tokenizer.next();
break;
}
Object val = parseValue(ordered);
result.add(val);
if (lastToken == Token.TokenComma) {
lastToken = tokenizer.next();
} else if (lastToken != Token.TokenBrackR) {
throw new JSONException("expecting either ',' or ']' " + tokenizer.onLineCol());