com.jsoniter.any.ArrayAny Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsoniter Show documentation
Show all versions of jsoniter Show documentation
jsoniter (json-iterator) is fast and flexible JSON parser available in Java and Go
package com.jsoniter.any;
import com.jsoniter.ValueType;
import com.jsoniter.output.JsonStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
class ArrayAny extends Any {
private final List val;
public ArrayAny(List val) {
this.val = val;
}
@Override
public ValueType valueType() {
return ValueType.ARRAY;
}
@Override
public Object object() {
return val;
}
@Override
public void writeTo(JsonStream stream) throws IOException {
stream.writeArrayStart();
Iterator iter = val.iterator();
if (!iter.hasNext()) {
stream.writeArrayEnd();
return;
}
iter.next().writeTo(stream);
while (iter.hasNext()) {
stream.writeMore();
iter.next().writeTo(stream);
}
stream.writeArrayEnd();
}
@Override
public int size() {
return val.size();
}
@Override
public Iterator iterator() {
return val.iterator();
}
@Override
public Any get(int index) {
try {
return val.get(index);
} catch (IndexOutOfBoundsException e) {
return new NotFoundAny(index, object());
}
}
@Override
public Any get(Object[] keys, int idx) {
if (idx == keys.length) {
return this;
}
Object key = keys[idx];
if (isWildcard(key)) {
ArrayList result = new ArrayList();
for (Any element : val) {
Any mapped = element.get(keys, idx + 1);
if (mapped.valueType() != ValueType.INVALID) {
result.add(mapped);
}
}
return Any.rewrap(result);
}
try {
return val.get((Integer) key).get(keys, idx + 1);
} catch (IndexOutOfBoundsException e) {
return new NotFoundAny(keys, idx, object());
} catch (ClassCastException e) {
return new NotFoundAny(keys, idx, object());
}
}
@Override
public String toString() {
return JsonStream.serialize(this);
}
@Override
public boolean toBoolean() {
return !val.isEmpty();
}
@Override
public int toInt() {
return val.size();
}
@Override
public long toLong() {
return val.size();
}
@Override
public float toFloat() {
return val.size();
}
@Override
public double toDouble() {
return val.size();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy