Core Jackson processing abstractions (aka Streaming API), implementation for JSON
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Jackson Core provides the fundamental streaming JSON parsing and generation capabilities for the Jackson data processing toolkit. It offers low-level incremental processing through a streaming API with JsonParser for reading and JsonGenerator for writing JSON content, serving as the foundation for all Jackson-based JSON processing including data-binding and alternative data formats.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.19.0</version>
</dependency>import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonToken;import com.fasterxml.jackson.core.*;
import java.io.*;
public class BasicExample {
public static void main(String[] args) throws Exception {
// Create a reusable JsonFactory
JsonFactory factory = JsonFactory.builder()
.enable(JsonReadFeature.ALLOW_JAVA_COMMENTS)
.build();
// Reading JSON
String jsonInput = "{\"name\":\"John\",\"age\":30,\"active\":true}";
JsonParser parser = factory.createParser(jsonInput);
while (parser.nextToken() != JsonToken.END_OBJECT) {
if (parser.getCurrentToken() == JsonToken.FIELD_NAME) {
String fieldName = parser.getCurrentName();
parser.nextToken();
switch (fieldName) {
case "name":
String name = parser.getValueAsString();
System.out.println("Name: " + name);
break;
case "age":
int age = parser.getValueAsInt();
System.out.println("Age: " + age);
break;
case "active":
boolean active = parser.getValueAsBoolean();
System.out.println("Active: " + active);
break;
}
}
}
parser.close();
// Writing JSON
StringWriter stringWriter = new StringWriter();
JsonGenerator generator = factory.createGenerator(stringWriter);
generator.writeStartObject();
generator.writeStringField("name", "Jane");
generator.writeNumberField("age", 25);
generator.writeBooleanField("active", false);
generator.writeEndObject();
generator.close();
System.out.println("Generated JSON: " + stringWriter.toString());
}
}Jackson Core is built around several key components:
Central factory class for creating JSON processors with comprehensive configuration options including features, constraints, and buffer management.
public class JsonFactory extends TokenStreamFactory implements java.io.Serializable {
public JsonParser createParser(String content) throws IOException;
public JsonParser createParser(InputStream in) throws IOException;
public JsonParser createParser(Reader r) throws IOException;
public JsonParser createParser(byte[] data) throws IOException;
public JsonParser createParser(File f) throws IOException;
public JsonGenerator createGenerator(OutputStream out) throws IOException;
public JsonGenerator createGenerator(Writer w) throws IOException;
public JsonGenerator createGenerator(File f, JsonEncoding enc) throws IOException;
}
public static class JsonFactoryBuilder extends TSFBuilder<JsonFactory, JsonFactoryBuilder> {
public JsonFactoryBuilder enable(JsonFactory.Feature f);
public JsonFactoryBuilder disable(JsonFactory.Feature f);
public JsonFactoryBuilder streamReadConstraints(StreamReadConstraints src);
public JsonFactoryBuilder streamWriteConstraints(StreamWriteConstraints swc);
public JsonFactory build();
}Streaming JSON parser for token-based processing with support for all JSON data types, numeric coercion, and configurable parsing features.
public abstract class JsonParser implements Closeable, Versioned {
public abstract JsonToken nextToken() throws IOException;
public abstract JsonToken getCurrentToken();
public abstract String getCurrentName() throws IOException;
public abstract String getText() throws IOException;
public abstract String getValueAsString() throws IOException;
public abstract int getValueAsInt() throws IOException;
public abstract long getValueAsLong() throws IOException;
public abstract double getValueAsDouble() throws IOException;
public abstract boolean getValueAsBoolean() throws IOException;
public abstract BigInteger getBigIntegerValue() throws IOException;
public abstract BigDecimal getDecimalValue() throws IOException;
public abstract NumberType getNumberType() throws IOException;
}
public enum JsonToken {
NOT_AVAILABLE, START_OBJECT, END_OBJECT, START_ARRAY, END_ARRAY,
FIELD_NAME, VALUE_STRING, VALUE_NUMBER_INT, VALUE_NUMBER_FLOAT,
VALUE_TRUE, VALUE_FALSE, VALUE_NULL
}Streaming JSON generator for writing JSON content with support for all JSON data types, pretty printing, and configurable output features.
public abstract class JsonGenerator implements Closeable, Flushable, Versioned {
public abstract void writeStartObject() throws IOException;
public abstract void writeEndObject() throws IOException;
public abstract void writeStartArray() throws IOException;
public abstract void writeEndArray() throws IOException;
public abstract void writeFieldName(String name) throws IOException;
public abstract void writeString(String text) throws IOException;
public abstract void writeNumber(int i) throws IOException;
public abstract void writeNumber(long l) throws IOException;
public abstract void writeNumber(double d) throws IOException;
public abstract void writeNumber(BigDecimal dec) throws IOException;
public abstract void writeBoolean(boolean state) throws IOException;
public abstract void writeNull() throws IOException;
public void writeStringField(String fieldName, String value) throws IOException;
public void writeNumberField(String fieldName, int value) throws IOException;
public void writeBooleanField(String fieldName, boolean value) throws IOException;
}Comprehensive feature system for configuring parsing and generation behavior, including JSON-specific features and format-agnostic capabilities.
public enum StreamReadFeature implements JacksonFeature {
AUTO_CLOSE_SOURCE, STRICT_DUPLICATE_DETECTION, IGNORE_UNDEFINED,
INCLUDE_SOURCE_IN_LOCATION, USE_FAST_DOUBLE_PARSER, USE_FAST_BIG_NUMBER_PARSER
}
public enum StreamWriteFeature implements JacksonFeature {
AUTO_CLOSE_TARGET, AUTO_CLOSE_JSON_CONTENT, FLUSH_PASSED_TO_STREAM,
WRITE_BIGDECIMAL_AS_PLAIN, STRICT_DUPLICATE_DETECTION, IGNORE_UNKNOWN
}
public final class StreamReadConstraints implements Serializable {
public static Builder builder();
public int getMaxStringLength();
public int getMaxNumberLength();
public int getMaxNestingDepth();
public long getMaxDocumentLength();
public int getMaxNameLength();
}Comprehensive exception hierarchy for JSON processing errors with detailed error reporting and location information.
public abstract class JacksonException extends IOException {
public abstract JsonLocation getLocation();
public abstract String getOriginalMessage();
public abstract Object getProcessor();
}
public abstract class JsonProcessingException extends JacksonException {
protected JsonProcessingException(String msg);
protected JsonProcessingException(String msg, JsonLocation loc);
protected JsonProcessingException(String msg, Throwable rootCause);
}
public class JsonParseException extends JsonProcessingException {
public JsonParseException(JsonParser p, String msg);
public JsonParseException(JsonParser p, String msg, Throwable rootCause);
}Utility classes for buffer management, pretty printing, filtering, and advanced processing patterns including parser delegation and generator decoration.
public class JsonParserDelegate extends JsonParser {
protected JsonParser delegate;
public JsonParserDelegate(JsonParser d);
public JsonParser getDelegate();
}
public class JsonGeneratorDelegate extends JsonGenerator {
protected JsonGenerator delegate;
public JsonGeneratorDelegate(JsonGenerator d);
public JsonGenerator getDelegate();
}
public interface PrettyPrinter {
void writeRootValueSeparator(JsonGenerator gen) throws IOException;
void writeStartObject(JsonGenerator gen) throws IOException;
void writeObjectFieldValueSeparator(JsonGenerator gen) throws IOException;
void writeEndObject(JsonGenerator gen, int nrOfEntries) throws IOException;
}Utilities and Advanced Features
public class JsonLocation implements Serializable {
public static final JsonLocation NA;
public Object getSourceRef();
public int getLineNr();
public int getColumnNr();
public long getCharOffset();
public long getByteOffset();
}
public abstract class JsonStreamContext {
public abstract JsonStreamContext getParent();
public abstract String getCurrentName();
public abstract boolean inObject();
public abstract boolean inArray();
public abstract boolean inRoot();
public abstract int getCurrentIndex();
public abstract int getEntryCount();
}
public enum JsonEncoding {
UTF8("UTF-8"), UTF16_BE("UTF-16BE"), UTF16_LE("UTF-16LE"), UTF32_BE("UTF-32BE"), UTF32_LE("UTF-32LE");
public String getJavaName();
public boolean isBigEndian();
}public enum NumberType {
INT, LONG, BIG_INTEGER, FLOAT, DOUBLE, BIG_DECIMAL
}
public enum NumberTypeFP {
UNKNOWN, FLOAT32, FLOAT64, BIG_DECIMAL
}public class JsonPointer implements Serializable {
public static final JsonPointer empty();
public static JsonPointer compile(String input) throws IllegalArgumentException;
public static JsonPointer valueOf(String input);
public JsonPointer append(JsonPointer tail);
public String toString();
public boolean matches();
}
public abstract class TypeReference<T> {
protected TypeReference();
public Type getType();
}