Core Jackson processing abstractions (aka Streaming API), implementation for JSON
—
Jackson Core provides extensive utility classes and advanced features for specialized use cases including parser/generator delegation, filtering, buffer management, and async processing.
public class JsonParserDelegate extends JsonParser {
protected JsonParser delegate;
public JsonParserDelegate(JsonParser d);
public JsonParser getDelegate();
public void setDelegate(JsonParser d);
// All JsonParser methods are delegated to the underlying parser
// Override specific methods to customize behavior
}public class JsonGeneratorDelegate extends JsonGenerator {
protected JsonGenerator delegate;
protected boolean delegateCopyMethods;
public JsonGeneratorDelegate(JsonGenerator d);
public JsonGeneratorDelegate(JsonGenerator d, boolean delegateCopyMethods);
public JsonGenerator getDelegate();
public void setDelegate(JsonGenerator d);
// All JsonGenerator methods are delegated to the underlying generator
// Override specific methods to customize behavior
}public interface JsonGeneratorDecorator {
JsonGenerator decorate(JsonFactory factory, JsonGenerator generator) throws IOException;
}public class JsonParserSequence extends JsonParserDelegate {
public static JsonParserSequence createFlattened(boolean checkForExistingToken, JsonParser first, JsonParser second);
public static JsonParserSequence createFlattened(boolean checkForExistingToken, JsonParser[] parsers);
public void addFlattenedActiveParsers(List<JsonParser> listToAddIn);
public int containedParsersCount();
@Override
public void close() throws IOException;
@Override
public JsonToken nextToken() throws IOException;
}public abstract class TokenFilter {
public static final TokenFilter INCLUDE_ALL;
public abstract TokenFilter includeElement(int index);
public abstract TokenFilter includeProperty(String name);
public abstract boolean includeValue(JsonParser p) throws IOException;
public abstract boolean includeRootValue(int index);
public boolean _includeScalar();
protected boolean _includeScalar(JsonParser p) throws IOException;
}public class JsonPointerBasedFilter extends TokenFilter {
protected final JsonPointer _pathToMatch;
public JsonPointerBasedFilter(String pathAsString);
public JsonPointerBasedFilter(JsonPointer match);
@Override
public TokenFilter includeElement(int index);
@Override
public TokenFilter includeProperty(String name);
@Override
public boolean includeValue(JsonParser p) throws IOException;
@Override
public String toString();
}public class FilteringParserDelegate extends JsonParserDelegate {
public enum Inclusion {
INCLUDE_ALL_AND_PATH,
INCLUDE_NON_NULL,
ONLY_INCLUDE_ALL
}
protected TokenFilter rootFilter;
protected boolean _allowMultipleMatches;
protected boolean _includePath;
protected boolean _includeImmediateParent;
public FilteringParserDelegate(JsonParser p, TokenFilter f, boolean includePath, boolean allowMultipleMatches);
public FilteringParserDelegate(JsonParser p, TokenFilter f, Inclusion inclusion, boolean allowMultipleMatches);
public TokenFilter getFilter();
public int getMatchCount();
@Override
public JsonToken nextToken() throws IOException;
@Override
public JsonToken getCurrentToken();
@Override
public String getCurrentName() throws IOException;
}public class FilteringGeneratorDelegate extends JsonGeneratorDelegate {
protected TokenFilter rootFilter;
protected boolean _allowMultipleMatches;
protected boolean _includePath;
protected boolean _includeImmediateParent;
public FilteringGeneratorDelegate(JsonGenerator d, TokenFilter f, boolean includePath, boolean allowMultipleMatches);
public FilteringGeneratorDelegate(JsonGenerator d, TokenFilter f, Inclusion inclusion, boolean allowMultipleMatches);
public TokenFilter getFilter();
public int getMatchCount();
// Override write methods to apply filtering
}public class BufferRecycler {
public enum BufferType {
BYTE_READ_IO_BUFFER(0, 8000),
BYTE_WRITE_ENCODING_BUFFER(1, 8000),
BYTE_WRITE_CONCAT_BUFFER(2, 2000),
CHAR_TOKEN_BUFFER(3, 2000),
CHAR_CONCAT_BUFFER(4, 2000),
CHAR_TEXT_BUFFER(5, 200),
CHAR_NAME_COPY_BUFFER(6, 200);
public int getId();
public int getDefaultSize();
}
public byte[] allocByteBuffer(BufferType type);
public byte[] allocByteBuffer(BufferType type, int minSize);
public void releaseByteBuffer(BufferType type, byte[] buffer);
public char[] allocCharBuffer(BufferType type);
public char[] allocCharBuffer(BufferType type, int minSize);
public void releaseCharBuffer(BufferType type, char[] buffer);
}public class BufferRecyclers {
public static BufferRecycler getBufferRecycler();
public static RecyclerPool<BufferRecycler> getBufferRecyclerPool();
public static void releaseBuffers();
public static void setBufferRecyclerPool(RecyclerPool<BufferRecycler> pool);
}public interface RecyclerPool<T> {
T acquirePooled();
void releasePooled(T pooled);
public static class BoundedPoolBase<P> implements RecyclerPool<P> {
protected final int _maxPooled;
public BoundedPoolBase(int poolSize);
public int capacity();
}
public static final class ThreadLocalPool<P> extends BoundedPoolBase<P> {
public ThreadLocalPool(int poolSize);
public ThreadLocalPool(int poolSize, Function<RecyclerPool<P>, P> creator);
}
public static final class LockFreePool<P> extends BoundedPoolBase<P> {
public LockFreePool(int poolSize);
public LockFreePool(int poolSize, Function<RecyclerPool<P>, P> creator);
}
public static final class ConcurrentDequePool<P> extends BoundedPoolBase<P> {
public ConcurrentDequePool(int poolSize);
public ConcurrentDequePool(int poolSize, Function<RecyclerPool<P>, P> creator);
}
public static final class NonRecyclingPool<P> implements RecyclerPool<P> {
public NonRecyclingPool();
public NonRecyclingPool(Function<RecyclerPool<P>, P> creator);
}
}public final class TextBuffer {
public TextBuffer(BufferRecycler allocator);
public TextBuffer(BufferRecycler allocator, int initialSize);
public void releaseBuffers();
public void resetWithEmpty();
public void resetWith(char c);
public void resetWithShared(char[] buf, int offset, int len);
public void resetWithCopy(char[] buf, int offset, int len);
public void resetWithCopy(String text, int start, int len);
public void resetWithString(String value);
public char[] getTextBuffer();
public int getTextOffset();
public int size();
public String contentsAsString();
public char[] contentsAsArray();
public BigDecimal contentsAsDecimal() throws NumberFormatException;
public double contentsAsDouble() throws NumberFormatException;
public int contentsAsInt(boolean neg) throws NumberFormatException;
public long contentsAsLong(boolean neg) throws NumberFormatException;
public char[] getCurrentSegment();
public char[] emptyAndGetCurrentSegment();
public int getCurrentSegmentSize();
public void setCurrentLength(int len);
public char[] finishCurrentSegment();
public char[] expandCurrentSegment();
public char[] expandCurrentSegment(int minSize);
public void append(char c);
public void append(char[] c, int start, int len);
public void append(String str, int offset, int len);
public void appendString(String str);
public String toString();
}public final class ByteArrayBuilder extends OutputStream {
public ByteArrayBuilder();
public ByteArrayBuilder(BufferRecycler br);
public ByteArrayBuilder(BufferRecycler br, int firstBlockSize);
public ByteArrayBuilder(int firstBlockSize);
public void reset();
public void release();
public int size();
public byte[] toByteArray();
public byte[] resetAndGetFirstSegment();
public void write(byte[] b);
public void write(byte[] b, int off, int len);
public void write(int b);
public void writeBytes(byte[] b, int off, int len);
public void close();
public void flush();
public String toString();
}public interface NonBlockingInputFeeder {
boolean needMoreInput();
void feedInput(byte[] buf, int start, int end) throws IOException;
void endOfInput();
}public interface ByteArrayFeeder extends NonBlockingInputFeeder {
void feedInput(byte[] buf, int start, int end) throws IOException;
}public interface ByteBufferFeeder extends NonBlockingInputFeeder {
void feedInput(ByteBuffer buffer) throws IOException;
}public class Separators implements Serializable {
public static final Separators DEFAULT;
public Separators();
public Separators(char objectFieldValueSeparator, char objectEntrySeparator, char arrayValueSeparator);
public Separators withObjectFieldValueSeparator(char sep);
public Separators withObjectEntrySeparator(char sep);
public Separators withArrayValueSeparator(char sep);
public char getObjectFieldValueSeparator();
public char getObjectEntrySeparator();
public char getArrayValueSeparator();
}public class MinimalPrettyPrinter implements PrettyPrinter, Serializable {
public MinimalPrettyPrinter();
public MinimalPrettyPrinter(String rootSeparator);
public void setRootValueSeparator(String sep);
@Override
public void writeRootValueSeparator(JsonGenerator gen) throws IOException;
@Override
public void writeStartObject(JsonGenerator gen) throws IOException;
@Override
public void writeEndObject(JsonGenerator gen, int nrOfEntries) throws IOException;
@Override
public void writeObjectEntryValueSeparator(JsonGenerator gen) throws IOException;
@Override
public void writeObjectFieldValueSeparator(JsonGenerator gen) throws IOException;
@Override
public void writeStartArray(JsonGenerator gen) throws IOException;
@Override
public void writeEndArray(JsonGenerator gen, int nrOfValues) throws IOException;
@Override
public void writeArrayValueSeparator(JsonGenerator gen) throws IOException;
@Override
public void beforeArrayValues(JsonGenerator gen) throws IOException;
@Override
public void beforeObjectEntries(JsonGenerator gen) throws IOException;
}public class LoggingParserDelegate extends JsonParserDelegate {
private static final Logger logger = LoggerFactory.getLogger(LoggingParserDelegate.class);
public LoggingParserDelegate(JsonParser delegate) {
super(delegate);
}
@Override
public JsonToken nextToken() throws IOException {
JsonToken token = super.nextToken();
if (token != null) {
logger.debug("Token: {} at {}", token, getCurrentLocation());
}
return token;
}
@Override
public String getText() throws IOException {
String text = super.getText();
logger.debug("Text value: '{}'", text);
return text;
}
}
// Usage
JsonParser parser = new LoggingParserDelegate(factory.createParser(input));// Parse only specific paths from large JSON
JsonPointer pathToExtract = JsonPointer.compile("/users/0/profile/name");
TokenFilter filter = new JsonPointerBasedFilter(pathToExtract);
JsonParser parser = new FilteringParserDelegate(
factory.createParser(largeJsonInput),
filter,
false, // don't include path
false // single match only
);
while (parser.nextToken() != null) {
if (parser.getCurrentToken() == JsonToken.VALUE_STRING) {
String extractedName = parser.getText();
System.out.println("Found name: " + extractedName);
break;
}
}// Custom buffer recycling strategy
RecyclerPool<BufferRecycler> customPool = new RecyclerPool.ThreadLocalPool<>(10);
BufferRecyclers.setBufferRecyclerPool(customPool);
// Or disable recycling entirely
BufferRecyclers.setBufferRecyclerPool(new RecyclerPool.NonRecyclingPool<>());JsonFactory factory = new JsonFactory();
JsonParser parser = factory.createNonBlockingByteArrayParser();
ByteArrayFeeder feeder = (ByteArrayFeeder) parser.getNonBlockingInputFeeder();
// Feed data in chunks
byte[] chunk1 = "{\"name\":\"John\",".getBytes();
byte[] chunk2 = "\"age\":30}".getBytes();
feeder.feedInput(chunk1, 0, chunk1.length);
JsonToken token;
while ((token = parser.nextToken()) == JsonToken.NOT_AVAILABLE) {
// Need more input
}
feeder.feedInput(chunk2, 0, chunk2.length);
feeder.endOfInput();
// Continue parsing
while ((token = parser.nextToken()) != null) {
// Process tokens
}public void processLargeJsonArray(InputStream input, Consumer<JsonNode> processor) throws IOException {
JsonFactory factory = JsonFactory.builder()
.disable(JsonFactory.Feature.INTERN_FIELD_NAMES) // Save memory
.disable(StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION) // Save memory
.build();
try (JsonParser parser = factory.createParser(input)) {
parser.nextToken(); // START_ARRAY
while (parser.nextToken() == JsonToken.START_OBJECT) {
JsonNode node = parser.readValueAsTree();
processor.accept(node);
// Node is automatically garbage collected after processing
}
}
}Install with Tessl CLI
npx tessl i tessl/maven-com-fasterxml-jackson-core--jackson-core