All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dslplatform.json.runtime.CollectionDecoder Maven / Gradle / Ivy

There is a newer version: 1.10.0
Show newest version
package com.dslplatform.json.runtime;

import com.dslplatform.json.JsonReader;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.concurrent.Callable;

public final class CollectionDecoder> implements JsonReader.ReadObject {

	private final Type manifest;
	private final Callable newInstance;
	private final JsonReader.ReadObject decoder;

	public CollectionDecoder(
			final Type manifest,
			final Callable newInstance,
			final JsonReader.ReadObject decoder) {
		if (manifest == null) throw new IllegalArgumentException("manifest can't be null");
		if (newInstance == null) throw new IllegalArgumentException("create can't be null");
		if (decoder == null) throw new IllegalArgumentException("decoder can't be null");
		this.manifest = manifest;
		this.newInstance = newInstance;
		this.decoder = decoder;
	}

	@Override
	public T read(final JsonReader reader) throws IOException {
		if (reader.wasNull()) return null;
		if (reader.last() != '[') {
			throw new java.io.IOException("Expecting '[' at position " + reader.positionInStream() + ". Found " + (char)reader.last());
		}
		final T instance;
		try {
			instance = newInstance.call();
		} catch (Exception e) {
			throw new IOException("Unable to create a new instance of " + manifest, e);
		}
		if (reader.getNextToken() == ']') return instance;
		instance.add(decoder.read(reader));
		while (reader.getNextToken() == ','){
			reader.getNextToken();
			instance.add(decoder.read(reader));
		}
		if (reader.last() != ']') {
			throw new java.io.IOException("Expecting ']' at position " + reader.positionInStream() + ". Found " + (char)reader.last());
		}
		return instance;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy