com.dslplatform.json.runtime.ArrayDecoder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dsl-json-java8 Show documentation
Show all versions of dsl-json-java8 Show documentation
DSL Platform compatible Java JSON library (https://dsl-platform.com)
package com.dslplatform.json.runtime;
import com.dslplatform.json.JsonReader;
import com.dslplatform.json.Nullable;
import java.io.IOException;
import java.util.ArrayList;
public final class ArrayDecoder implements JsonReader.ReadObject {
private final T[] emptyInstance;
private final JsonReader.ReadObject decoder;
public ArrayDecoder(
final T[] emptyInstance,
final JsonReader.ReadObject decoder) {
if (emptyInstance == null) throw new IllegalArgumentException("emptyInstance can't be null");
if (decoder == null) throw new IllegalArgumentException("decoder can't be null");
this.emptyInstance = emptyInstance;
this.decoder = decoder;
}
@Nullable
@Override
public T[] read(final JsonReader reader) throws IOException {
if (reader.wasNull()) return null;
if (reader.last() != '[') {
throw new IOException("Expecting '[' " + reader.positionDescription() + ". Found " + (char)reader.last());
}
if (reader.getNextToken() == ']') return emptyInstance;
final ArrayList list = new ArrayList<>(4);
list.add(decoder.read(reader));
while (reader.getNextToken() == ','){
reader.getNextToken();
list.add(decoder.read(reader));
}
if (reader.last() != ']') {
throw new IOException("Expecting ']' " + reader.positionDescription() + ". Found " + (char)reader.last());
}
return list.toArray(emptyInstance);
}
}