Core Jackson processing abstractions (aka Streaming API), implementation for JSON
—
Jackson Core provides extensive configuration options through feature enums and constraint classes, allowing fine-tuned control over parsing and generation behavior for performance, security, and compatibility requirements.
public enum StreamReadFeature implements JacksonFeature {
AUTO_CLOSE_SOURCE(true),
STRICT_DUPLICATE_DETECTION(false),
IGNORE_UNDEFINED(false),
INCLUDE_SOURCE_IN_LOCATION(true),
USE_FAST_DOUBLE_PARSER(true),
USE_FAST_BIG_NUMBER_PARSER(true);
public boolean enabledByDefault();
public boolean enabledIn(int flags);
public int getMask();
}Feature Details:
AUTO_CLOSE_SOURCE: Automatically close input sources when parser is closedSTRICT_DUPLICATE_DETECTION: Detect and reject duplicate JSON object keysIGNORE_UNDEFINED: Ignore undefined values during parsingINCLUDE_SOURCE_IN_LOCATION: Include source reference in location informationUSE_FAST_DOUBLE_PARSER: Use optimized double parsing for better performanceUSE_FAST_BIG_NUMBER_PARSER: Use optimized BigDecimal/BigInteger parsingpublic enum StreamWriteFeature implements JacksonFeature {
AUTO_CLOSE_TARGET(true),
AUTO_CLOSE_JSON_CONTENT(true),
FLUSH_PASSED_TO_STREAM(true),
WRITE_BIGDECIMAL_AS_PLAIN(false),
STRICT_DUPLICATE_DETECTION(false),
IGNORE_UNKNOWN(false);
public boolean enabledByDefault();
public boolean enabledIn(int flags);
public int getMask();
}Feature Details:
AUTO_CLOSE_TARGET: Automatically close output targets when generator is closedAUTO_CLOSE_JSON_CONTENT: Automatically close incomplete JSON structuresFLUSH_PASSED_TO_STREAM: Pass flush() calls to underlying output streamWRITE_BIGDECIMAL_AS_PLAIN: Write BigDecimal using plain notation (no scientific)STRICT_DUPLICATE_DETECTION: Detect and reject duplicate field names during generationIGNORE_UNKNOWN: Ignore unknown configuration settingspublic enum JsonReadFeature implements FormatFeature {
ALLOW_JAVA_COMMENTS(false),
ALLOW_YAML_COMMENTS(false),
ALLOW_SINGLE_QUOTES(false),
ALLOW_UNQUOTED_FIELD_NAMES(false),
ALLOW_UNESCAPED_CONTROL_CHARS(false),
ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER(false),
ALLOW_NUMERIC_LEADING_ZEROS(false),
ALLOW_NON_NUMERIC_NUMBERS(false),
ALLOW_MISSING_VALUES(false),
ALLOW_TRAILING_COMMA(false),
ALLOW_TRAILING_DECIMAL_POINT_FOR_NUMBERS(false),
ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS(false),
ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS(false);
public boolean enabledByDefault();
public boolean enabledIn(int flags);
public int getMask();
}Feature Details:
ALLOW_JAVA_COMMENTS: Accept Java/C++ style comments (// and /* */)ALLOW_YAML_COMMENTS: Accept YAML style comments (#)ALLOW_SINGLE_QUOTES: Accept single quotes around strings and field namesALLOW_UNQUOTED_FIELD_NAMES: Accept unquoted field namesALLOW_UNESCAPED_CONTROL_CHARS: Accept unescaped control characters in stringsALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER: Allow backslash escaping of any characterALLOW_NUMERIC_LEADING_ZEROS: Accept numbers with leading zerosALLOW_NON_NUMERIC_NUMBERS: Accept special float values (NaN, Infinity)ALLOW_MISSING_VALUES: Accept missing values in arrays/objectsALLOW_TRAILING_COMMA: Accept trailing commas in arrays and objectsALLOW_TRAILING_DECIMAL_POINT_FOR_NUMBERS: Accept trailing decimal pointsALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS: Accept leading decimal pointsALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS: Accept leading plus signs in numberspublic enum JsonWriteFeature implements FormatFeature {
QUOTE_FIELD_NAMES(true),
WRITE_NAN_AS_STRINGS(false),
WRITE_NUMBERS_AS_STRINGS(false),
ESCAPE_NON_ASCII(false),
WRITE_BIGDECIMAL_AS_PLAIN(false);
public boolean enabledByDefault();
public boolean enabledIn(int flags);
public int getMask();
}Feature Details:
QUOTE_FIELD_NAMES: Quote JSON field names (standard JSON)WRITE_NAN_AS_STRINGS: Write NaN and Infinity as quoted stringsWRITE_NUMBERS_AS_STRINGS: Write all numeric values as quoted stringsESCAPE_NON_ASCII: Escape all non-ASCII charactersWRITE_BIGDECIMAL_AS_PLAIN: Write BigDecimal values in plain notationpublic final class StreamReadConstraints implements Serializable {
public static final int DEFAULT_MAX_STRING_LEN = 20_000_000;
public static final int DEFAULT_MAX_NUMBER_LEN = 1000;
public static final int DEFAULT_MAX_DEPTH = 1000;
public static final long DEFAULT_MAX_DOC_LEN = -1L;
public static final int DEFAULT_MAX_NAME_LEN = 50000;
public static Builder builder();
public static StreamReadConstraints defaults();
public int getMaxStringLength();
public int getMaxNumberLength();
public int getMaxNestingDepth();
public long getMaxDocumentLength();
public int getMaxNameLength();
public static class Builder {
public Builder maxStringLength(int maxStringLength);
public Builder maxNumberLength(int maxNumberLength);
public Builder maxNestingDepth(int maxNestingDepth);
public Builder maxDocumentLength(long maxDocumentLength);
public Builder maxNameLength(int maxNameLength);
public StreamReadConstraints build();
}
}public final class StreamWriteConstraints implements Serializable {
public static final int DEFAULT_MAX_DEPTH = 1000;
public static Builder builder();
public static StreamWriteConstraints defaults();
public int getMaxNestingDepth();
public static class Builder {
public Builder maxNestingDepth(int maxNestingDepth);
public StreamWriteConstraints build();
}
}public enum Feature implements JacksonFeature {
INTERN_FIELD_NAMES(true),
CANONICALIZE_FIELD_NAMES(true),
FAIL_ON_SYMBOL_HASH_OVERFLOW(true),
USE_FAST_DOUBLE_PARSER(true),
USE_FAST_BIG_NUMBER_PARSER(true),
USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING(true);
public boolean enabledByDefault();
public boolean enabledIn(int flags);
public int getMask();
}Feature Details:
INTERN_FIELD_NAMES: Intern field names for memory efficiencyCANONICALIZE_FIELD_NAMES: Canonicalize field names (required for interning)FAIL_ON_SYMBOL_HASH_OVERFLOW: Fail when symbol table hash overflow detected (DoS protection)USE_FAST_DOUBLE_PARSER: Use fast double parser implementationUSE_FAST_BIG_NUMBER_PARSER: Use fast BigDecimal/BigInteger parserUSE_THREAD_LOCAL_FOR_BUFFER_RECYCLING: Use thread-local buffer recyclingpublic JsonParser enable(StreamReadFeature f);
public JsonParser disable(StreamReadFeature f);
public JsonParser configure(StreamReadFeature f, boolean state);
public boolean isEnabled(StreamReadFeature f);
public int getFeatureMask();
public JsonParser setFeatureMask(int mask);public JsonGenerator enable(StreamWriteFeature f);
public JsonGenerator disable(StreamWriteFeature f);
public JsonGenerator configure(StreamWriteFeature f, boolean state);
public boolean isEnabled(StreamWriteFeature f);
public int getFeatureMask();
public JsonGenerator setFeatureMask(int mask);JsonFactory factory = JsonFactory.builder()
.enable(JsonReadFeature.ALLOW_JAVA_COMMENTS)
.enable(JsonReadFeature.ALLOW_SINGLE_QUOTES)
.enable(JsonReadFeature.ALLOW_UNQUOTED_FIELD_NAMES)
.enable(JsonReadFeature.ALLOW_TRAILING_COMMA)
.build();
// Can now parse: {name: 'John', age: 30, /* comment */ active: true,}JsonFactory factory = JsonFactory.builder()
.enable(StreamReadFeature.STRICT_DUPLICATE_DETECTION)
.streamReadConstraints(StreamReadConstraints.builder()
.maxStringLength(100_000)
.maxNumberLength(100)
.maxNestingDepth(50)
.maxDocumentLength(1_000_000L)
.build())
.build();JsonFactory factory = JsonFactory.builder()
.enable(StreamReadFeature.USE_FAST_DOUBLE_PARSER)
.enable(StreamReadFeature.USE_FAST_BIG_NUMBER_PARSER)
.disable(StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION)
.disable(JsonFactory.Feature.INTERN_FIELD_NAMES)
.build();JsonFactory factory = JsonFactory.builder()
.enable(JsonWriteFeature.WRITE_BIGDECIMAL_AS_PLAIN)
.disable(JsonWriteFeature.QUOTE_FIELD_NAMES) // Non-standard JSON
.streamWriteConstraints(StreamWriteConstraints.builder()
.maxNestingDepth(100)
.build())
.build();JsonParser parser = factory.createParser(input);
parser.enable(StreamReadFeature.STRICT_DUPLICATE_DETECTION);
JsonGenerator generator = factory.createGenerator(output);
generator.disable(StreamWriteFeature.AUTO_CLOSE_JSON_CONTENT);// Save current feature state
int originalMask = parser.getFeatureMask();
// Temporarily modify features
parser.enable(StreamReadFeature.IGNORE_UNDEFINED)
.disable(StreamReadFeature.AUTO_CLOSE_SOURCE);
// Process with modified features
// ... parsing logic ...
// Restore original features
parser.setFeatureMask(originalMask);public final class ErrorReportConfiguration implements Serializable {
public static final int DEFAULT_MAX_RAW_CONTENT_LENGTH = 500;
public static final int DEFAULT_MAX_ERROR_TOKEN_LENGTH = 256;
public static Builder builder();
public static ErrorReportConfiguration defaults();
public int getMaxRawContentLength();
public int getMaxErrorTokenLength();
public static class Builder {
public Builder maxRawContentLength(int maxRawContentLength);
public Builder maxErrorTokenLength(int maxErrorTokenLength);
public ErrorReportConfiguration build();
}
}Example:
JsonFactory factory = JsonFactory.builder()
.errorReportConfiguration(ErrorReportConfiguration.builder()
.maxErrorTokenLength(100)
.maxRawContentLength(200)
.build())
.build();Install with Tessl CLI
npx tessl i tessl/maven-com-fasterxml-jackson-core--jackson-core