com.adgear.anoa.read.JacksonStreams Maven / Gradle / Ivy
Show all versions of anoa-core Show documentation
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(/*@NonNull*/ C objectCodec, /*@NonNull*/ Optional schema) {
this.objectCodec = objectCodec;
this.factory = (F) objectCodec.getFactory();
this.factory.setCodec(this.objectCodec);
this.schema = schema;
}
@SuppressWarnings("unchecked")
public /*@NonNull*/ P parser(/*@NonNull*/ InputStream inputStream) {
try {
return with((P) factory.createParser(new BufferedInputStream(inputStream)));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@SuppressWarnings("unchecked")
public /*@NonNull*/ P parser(/*@NonNull*/ Reader reader) {
try {
return with((P) factory.createParser(new BufferedReader(reader)));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@SuppressWarnings("unchecked")
public /*@NonNull*/ P parser(/*@NonNull*/ byte[] bytes) {
try {
return with((P) factory.createParser(bytes));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@SuppressWarnings("unchecked")
public /*@NonNull*/ P parser(/*@NonNull*/ String string) {
try {
return with((P) factory.createParser(string));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@SuppressWarnings("unchecked")
public /*@NonNull*/ P parser(/*@NonNull*/ File file) {
try {
return with((P) factory.createParser(file));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@SuppressWarnings("unchecked")
public /*@NonNull*/ P parser(/*@NonNull*/ URL url) {
try {
return with((P) factory.createParser(url));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
/**
* @return the parser passed as argument, after setting the current object mapper and schema
*/
public /*@NonNull*/ P with(/*@NonNull*/ P parser) {
parser.setCodec(objectCodec);
schema.ifPresent(parser::setSchema);
return parser;
}
public /*@NonNull*/ Stream from(/*@NonNull*/ P parser) {
return LookAheadIteratorFactory.jackson(parser).asStream();
}
public /*@NonNull*/ Stream from(/*@NonNull*/ InputStream inputStream) {
return from(parser(inputStream));
}
public /*@NonNull*/ Stream from(/*@NonNull*/ Reader reader) {
return from(parser(reader));
}
public /*@NonNull*/ Stream from(/*@NonNull*/ byte[] bytes) {
return from(parser(bytes));
}
public /*@NonNull*/ Stream from(/*@NonNull*/ String string) {
return from(parser(string));
}
public /*@NonNull*/ Stream from(/*@NonNull*/ File file) {
return from(parser(file));
}
public /*@NonNull*/ Stream from(/*@NonNull*/ URL url) {
return from(parser(url));
}
public /*@NonNull*/ Stream> from(/*@NonNull*/ AnoaHandler anoaHandler,
/*@NonNull*/ P parser) {
return LookAheadIteratorFactory.jackson(anoaHandler, parser).asStream();
}
public /*@NonNull*/ Stream> from(/*@NonNull*/ AnoaHandler anoaHandler,
/*@NonNull*/ InputStream inputStream) {
return from(anoaHandler, parser(inputStream));
}
public /*@NonNull*/ Stream> from(/*@NonNull*/ AnoaHandler anoaHandler,
/*@NonNull*/ Reader reader) {
return from(anoaHandler, parser(reader));
}
public /*@NonNull*/ Stream> from(/*@NonNull*/ AnoaHandler anoaHandler,
/*@NonNull*/ byte[] bytes) {
return from(anoaHandler, parser(bytes));
}
public /*@NonNull*/ Stream> from(/*@NonNull*/ AnoaHandler anoaHandler,
/*@NonNull*/ String string) {
return from(anoaHandler, parser(string));
}
public /*@NonNull*/ Stream> from(/*@NonNull*/ AnoaHandler anoaHandler,
/*@NonNull*/ File file) {
return from(anoaHandler, parser(file));
}
public /*@NonNull*/ Stream> from(/*@NonNull*/ AnoaHandler anoaHandler,
/*@NonNull*/ URL url) {
return from(anoaHandler, parser(url));
}
}