elemental.json.impl.JreJsonArray Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2010 Google Inc.
*
* 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 elemental.json.impl;
import java.util.ArrayList;
import java.util.List;
import elemental.json.JsonArray;
import elemental.json.JsonBoolean;
import elemental.json.JsonFactory;
import elemental.json.JsonNumber;
import elemental.json.JsonObject;
import elemental.json.JsonString;
import elemental.json.JsonType;
import elemental.json.JsonValue;
/**
* Server-side implementation of JsonArray.
*/
public class JreJsonArray extends JreJsonValue implements JsonArray {
private ArrayList arrayValues = new ArrayList();
private JsonFactory factory;
public JreJsonArray(JsonFactory factory) {
this.factory = factory;
}
@Override
public boolean asBoolean() {
return true;
}
@Override
public double asNumber() {
switch (length()) {
case 0:
return 0;
case 1:
return get(0).asNumber();
default:
return Double.NaN;
}
}
@Override
public String asString() {
StringBuilder toReturn = new StringBuilder();
for (int i = 0; i < length(); i++) {
if (i > 0) {
toReturn.append(", ");
}
toReturn.append(get(i).asString());
}
return toReturn.toString();
}
public JsonValue get(int index) {
return arrayValues.get(index);
}
public JsonArray getArray(int index) {
return (JsonArray) get(index);
}
public boolean getBoolean(int index) {
return ((JsonBoolean) get(index)).getBoolean();
}
public double getNumber(int index) {
return ((JsonNumber) get(index)).getNumber();
}
public JsonObject getObject(int index) {
return (JsonObject) get(index);
}
public Object getObject() {
List