com.adgear.anoa.read.JacksonStreams Maven / Gradle / Ivy
package com.adgear.anoa.read;
import com.adgear.anoa.Anoa;
import com.adgear.anoa.AnoaHandler;
import com.fasterxml.jackson.core.FormatSchema;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.core.TreeNode;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.net.URL;
import java.util.Optional;
import java.util.stream.Stream;
/**
* Utility class for deserializing Jackson {@link com.fasterxml.jackson.core.TreeNode} instances as
* a {@link java.util.stream.Stream}.
*
* Intended to be used as a base class, subclasses should wrap appropriate Jackson databinding types
* together; see the anoa-tools module for examples.
*
* @param Record type
* @param Mapper type
* @param Factory type
* @param Schema type
* @param Parser type
*/
public class JacksonStreams<
N extends TreeNode,
C extends ObjectCodec,
F extends JsonFactory,
S extends FormatSchema,
P extends JsonParser> {
/**
* The object mapper used by this instance
*/
final public C objectCodec;
/**
* The factory used by this instance
*/
final public F factory;
/**
* The (optional) format schema used by this instance
*/
final public Optional schema;
/**
* @param objectCodec Jackson object mapper instance
* @param schema Jackson format schema (optional)
*/
@SuppressWarnings("unchecked")
public JacksonStreams(C objectCodec, Optional schema) {
this.objectCodec = objectCodec;
this.factory = (F) objectCodec.getFactory();
this.factory.setCodec(this.objectCodec);
this.schema = schema;
}
@SuppressWarnings("unchecked")
protected P parserChecked(InputStream inputStream) throws IOException {
return with((P) factory.createParser(new BufferedInputStream(inputStream)));
}
public P parser(InputStream inputStream) {
try {
return parserChecked(inputStream);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@SuppressWarnings("unchecked")
protected P parserChecked(Reader reader) throws IOException {
return with((P) factory.createParser(new BufferedReader(reader)));
}
public P parser(Reader reader) {
try {
return parserChecked(reader);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@SuppressWarnings("unchecked")
protected P parserChecked(byte[] bytes) throws IOException {
return with((P) factory.createParser(bytes));
}
public P parser(byte[] bytes) {
try {
return parserChecked(bytes);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@SuppressWarnings("unchecked")
protected P parserChecked(String string) throws IOException {
return with((P) factory.createParser(string));
}
public P parser(String string) {
try {
return parserChecked(string);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@SuppressWarnings("unchecked")
protected P parserChecked(File file) throws IOException {
return with((P) factory.createParser(file));
}
public P parser(File file) {
try {
return parserChecked(file);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@SuppressWarnings("unchecked")
public P parserChecked(URL url) throws IOException {
return with((P) factory.createParser(url));
}
public P parser(URL url) {
try {
return parserChecked(url);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
/**
* @return the parser passed as argument, after setting the current object mapper and schema
*/
public P with(P parser) {
parser.setCodec(objectCodec);
schema.ifPresent(parser::setSchema);
return parser;
}
protected N decodeChecked(P parser) throws IOException {
return parser.readValueAsTree();
}
public N decode(P parser) {
try {
return decodeChecked(parser);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public Stream from(P parser) {
return LookAheadIteratorFactory.jackson(parser).asStream();
}
public Stream from(InputStream inputStream) {
return from(parser(inputStream));
}
public Stream from(Reader reader) {
return from(parser(reader));
}
public Stream from(byte[] bytes) {
return from(parser(bytes));
}
public Stream from(String string) {
return from(parser(string));
}
public Stream from(File file) {
return from(parser(file));
}
public Stream from(URL url) {
return from(parser(url));
}
public Stream> from(AnoaHandler anoaHandler, P parser) {
return LookAheadIteratorFactory.jackson(anoaHandler, parser).asStream();
}
public Stream> from(AnoaHandler anoaHandler, InputStream inputStream) {
return from(anoaHandler, parser(inputStream));
}
public Stream> from(AnoaHandler anoaHandler, Reader reader) {
return from(anoaHandler, parser(reader));
}
public Stream> from(AnoaHandler anoaHandler, byte[] bytes) {
return from(anoaHandler, parser(bytes));
}
public Stream> from(AnoaHandler anoaHandler, String string) {
return from(anoaHandler, parser(string));
}
public Stream> from(AnoaHandler anoaHandler, File file) {
return from(anoaHandler, parser(file));
}
public Stream> from(AnoaHandler anoaHandler, URL url) {
return from(anoaHandler, parser(url));
}
}