0
# Features and Configuration
1
2
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.
3
4
## Stream Features
5
6
### Stream Read Features
7
8
```java { .api }
9
public enum StreamReadFeature implements JacksonFeature {
10
AUTO_CLOSE_SOURCE(true),
11
STRICT_DUPLICATE_DETECTION(false),
12
IGNORE_UNDEFINED(false),
13
INCLUDE_SOURCE_IN_LOCATION(true),
14
USE_FAST_DOUBLE_PARSER(true),
15
USE_FAST_BIG_NUMBER_PARSER(true);
16
17
public boolean enabledByDefault();
18
public boolean enabledIn(int flags);
19
public int getMask();
20
}
21
```
22
23
**Feature Details:**
24
- `AUTO_CLOSE_SOURCE`: Automatically close input sources when parser is closed
25
- `STRICT_DUPLICATE_DETECTION`: Detect and reject duplicate JSON object keys
26
- `IGNORE_UNDEFINED`: Ignore undefined values during parsing
27
- `INCLUDE_SOURCE_IN_LOCATION`: Include source reference in location information
28
- `USE_FAST_DOUBLE_PARSER`: Use optimized double parsing for better performance
29
- `USE_FAST_BIG_NUMBER_PARSER`: Use optimized BigDecimal/BigInteger parsing
30
31
### Stream Write Features
32
33
```java { .api }
34
public enum StreamWriteFeature implements JacksonFeature {
35
AUTO_CLOSE_TARGET(true),
36
AUTO_CLOSE_JSON_CONTENT(true),
37
FLUSH_PASSED_TO_STREAM(true),
38
WRITE_BIGDECIMAL_AS_PLAIN(false),
39
STRICT_DUPLICATE_DETECTION(false),
40
IGNORE_UNKNOWN(false);
41
42
public boolean enabledByDefault();
43
public boolean enabledIn(int flags);
44
public int getMask();
45
}
46
```
47
48
**Feature Details:**
49
- `AUTO_CLOSE_TARGET`: Automatically close output targets when generator is closed
50
- `AUTO_CLOSE_JSON_CONTENT`: Automatically close incomplete JSON structures
51
- `FLUSH_PASSED_TO_STREAM`: Pass flush() calls to underlying output stream
52
- `WRITE_BIGDECIMAL_AS_PLAIN`: Write BigDecimal using plain notation (no scientific)
53
- `STRICT_DUPLICATE_DETECTION`: Detect and reject duplicate field names during generation
54
- `IGNORE_UNKNOWN`: Ignore unknown configuration settings
55
56
## JSON-Specific Features
57
58
### JSON Read Features
59
60
```java { .api }
61
public enum JsonReadFeature implements FormatFeature {
62
ALLOW_JAVA_COMMENTS(false),
63
ALLOW_YAML_COMMENTS(false),
64
ALLOW_SINGLE_QUOTES(false),
65
ALLOW_UNQUOTED_FIELD_NAMES(false),
66
ALLOW_UNESCAPED_CONTROL_CHARS(false),
67
ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER(false),
68
ALLOW_NUMERIC_LEADING_ZEROS(false),
69
ALLOW_NON_NUMERIC_NUMBERS(false),
70
ALLOW_MISSING_VALUES(false),
71
ALLOW_TRAILING_COMMA(false),
72
ALLOW_TRAILING_DECIMAL_POINT_FOR_NUMBERS(false),
73
ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS(false),
74
ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS(false);
75
76
public boolean enabledByDefault();
77
public boolean enabledIn(int flags);
78
public int getMask();
79
}
80
```
81
82
**Feature Details:**
83
- `ALLOW_JAVA_COMMENTS`: Accept Java/C++ style comments (`//` and `/* */`)
84
- `ALLOW_YAML_COMMENTS`: Accept YAML style comments (`#`)
85
- `ALLOW_SINGLE_QUOTES`: Accept single quotes around strings and field names
86
- `ALLOW_UNQUOTED_FIELD_NAMES`: Accept unquoted field names
87
- `ALLOW_UNESCAPED_CONTROL_CHARS`: Accept unescaped control characters in strings
88
- `ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER`: Allow backslash escaping of any character
89
- `ALLOW_NUMERIC_LEADING_ZEROS`: Accept numbers with leading zeros
90
- `ALLOW_NON_NUMERIC_NUMBERS`: Accept special float values (`NaN`, `Infinity`)
91
- `ALLOW_MISSING_VALUES`: Accept missing values in arrays/objects
92
- `ALLOW_TRAILING_COMMA`: Accept trailing commas in arrays and objects
93
- `ALLOW_TRAILING_DECIMAL_POINT_FOR_NUMBERS`: Accept trailing decimal points
94
- `ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS`: Accept leading decimal points
95
- `ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS`: Accept leading plus signs in numbers
96
97
### JSON Write Features
98
99
```java { .api }
100
public enum JsonWriteFeature implements FormatFeature {
101
QUOTE_FIELD_NAMES(true),
102
WRITE_NAN_AS_STRINGS(false),
103
WRITE_NUMBERS_AS_STRINGS(false),
104
ESCAPE_NON_ASCII(false),
105
WRITE_BIGDECIMAL_AS_PLAIN(false);
106
107
public boolean enabledByDefault();
108
public boolean enabledIn(int flags);
109
public int getMask();
110
}
111
```
112
113
**Feature Details:**
114
- `QUOTE_FIELD_NAMES`: Quote JSON field names (standard JSON)
115
- `WRITE_NAN_AS_STRINGS`: Write NaN and Infinity as quoted strings
116
- `WRITE_NUMBERS_AS_STRINGS`: Write all numeric values as quoted strings
117
- `ESCAPE_NON_ASCII`: Escape all non-ASCII characters
118
- `WRITE_BIGDECIMAL_AS_PLAIN`: Write BigDecimal values in plain notation
119
120
## Constraints
121
122
### Stream Read Constraints
123
124
```java { .api }
125
public final class StreamReadConstraints implements Serializable {
126
public static final int DEFAULT_MAX_STRING_LEN = 20_000_000;
127
public static final int DEFAULT_MAX_NUMBER_LEN = 1000;
128
public static final int DEFAULT_MAX_DEPTH = 1000;
129
public static final long DEFAULT_MAX_DOC_LEN = -1L;
130
public static final int DEFAULT_MAX_NAME_LEN = 50000;
131
132
public static Builder builder();
133
public static StreamReadConstraints defaults();
134
135
public int getMaxStringLength();
136
public int getMaxNumberLength();
137
public int getMaxNestingDepth();
138
public long getMaxDocumentLength();
139
public int getMaxNameLength();
140
141
public static class Builder {
142
public Builder maxStringLength(int maxStringLength);
143
public Builder maxNumberLength(int maxNumberLength);
144
public Builder maxNestingDepth(int maxNestingDepth);
145
public Builder maxDocumentLength(long maxDocumentLength);
146
public Builder maxNameLength(int maxNameLength);
147
public StreamReadConstraints build();
148
}
149
}
150
```
151
152
### Stream Write Constraints
153
154
```java { .api }
155
public final class StreamWriteConstraints implements Serializable {
156
public static final int DEFAULT_MAX_DEPTH = 1000;
157
158
public static Builder builder();
159
public static StreamWriteConstraints defaults();
160
161
public int getMaxNestingDepth();
162
163
public static class Builder {
164
public Builder maxNestingDepth(int maxNestingDepth);
165
public StreamWriteConstraints build();
166
}
167
}
168
```
169
170
## Factory Features
171
172
### JsonFactory Features
173
174
```java { .api }
175
public enum Feature implements JacksonFeature {
176
INTERN_FIELD_NAMES(true),
177
CANONICALIZE_FIELD_NAMES(true),
178
FAIL_ON_SYMBOL_HASH_OVERFLOW(true),
179
USE_FAST_DOUBLE_PARSER(true),
180
USE_FAST_BIG_NUMBER_PARSER(true),
181
USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING(true);
182
183
public boolean enabledByDefault();
184
public boolean enabledIn(int flags);
185
public int getMask();
186
}
187
```
188
189
**Feature Details:**
190
- `INTERN_FIELD_NAMES`: Intern field names for memory efficiency
191
- `CANONICALIZE_FIELD_NAMES`: Canonicalize field names (required for interning)
192
- `FAIL_ON_SYMBOL_HASH_OVERFLOW`: Fail when symbol table hash overflow detected (DoS protection)
193
- `USE_FAST_DOUBLE_PARSER`: Use fast double parser implementation
194
- `USE_FAST_BIG_NUMBER_PARSER`: Use fast BigDecimal/BigInteger parser
195
- `USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING`: Use thread-local buffer recycling
196
197
## Feature Management
198
199
### Parser Feature Control
200
201
```java { .api }
202
public JsonParser enable(StreamReadFeature f);
203
public JsonParser disable(StreamReadFeature f);
204
public JsonParser configure(StreamReadFeature f, boolean state);
205
public boolean isEnabled(StreamReadFeature f);
206
public int getFeatureMask();
207
public JsonParser setFeatureMask(int mask);
208
```
209
210
### Generator Feature Control
211
212
```java { .api }
213
public JsonGenerator enable(StreamWriteFeature f);
214
public JsonGenerator disable(StreamWriteFeature f);
215
public JsonGenerator configure(StreamWriteFeature f, boolean state);
216
public boolean isEnabled(StreamWriteFeature f);
217
public int getFeatureMask();
218
public JsonGenerator setFeatureMask(int mask);
219
```
220
221
## Usage Examples
222
223
### Lenient JSON Parsing
224
225
```java
226
JsonFactory factory = JsonFactory.builder()
227
.enable(JsonReadFeature.ALLOW_JAVA_COMMENTS)
228
.enable(JsonReadFeature.ALLOW_SINGLE_QUOTES)
229
.enable(JsonReadFeature.ALLOW_UNQUOTED_FIELD_NAMES)
230
.enable(JsonReadFeature.ALLOW_TRAILING_COMMA)
231
.build();
232
233
// Can now parse: {name: 'John', age: 30, /* comment */ active: true,}
234
```
235
236
### Strict Parsing with Constraints
237
238
```java
239
JsonFactory factory = JsonFactory.builder()
240
.enable(StreamReadFeature.STRICT_DUPLICATE_DETECTION)
241
.streamReadConstraints(StreamReadConstraints.builder()
242
.maxStringLength(100_000)
243
.maxNumberLength(100)
244
.maxNestingDepth(50)
245
.maxDocumentLength(1_000_000L)
246
.build())
247
.build();
248
```
249
250
### Performance-Optimized Configuration
251
252
```java
253
JsonFactory factory = JsonFactory.builder()
254
.enable(StreamReadFeature.USE_FAST_DOUBLE_PARSER)
255
.enable(StreamReadFeature.USE_FAST_BIG_NUMBER_PARSER)
256
.disable(StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION)
257
.disable(JsonFactory.Feature.INTERN_FIELD_NAMES)
258
.build();
259
```
260
261
### Generator Configuration
262
263
```java
264
JsonFactory factory = JsonFactory.builder()
265
.enable(JsonWriteFeature.WRITE_BIGDECIMAL_AS_PLAIN)
266
.disable(JsonWriteFeature.QUOTE_FIELD_NAMES) // Non-standard JSON
267
.streamWriteConstraints(StreamWriteConstraints.builder()
268
.maxNestingDepth(100)
269
.build())
270
.build();
271
```
272
273
### Runtime Feature Toggle
274
275
```java
276
JsonParser parser = factory.createParser(input);
277
parser.enable(StreamReadFeature.STRICT_DUPLICATE_DETECTION);
278
279
JsonGenerator generator = factory.createGenerator(output);
280
generator.disable(StreamWriteFeature.AUTO_CLOSE_JSON_CONTENT);
281
```
282
283
### Feature Mask Operations
284
285
```java
286
// Save current feature state
287
int originalMask = parser.getFeatureMask();
288
289
// Temporarily modify features
290
parser.enable(StreamReadFeature.IGNORE_UNDEFINED)
291
.disable(StreamReadFeature.AUTO_CLOSE_SOURCE);
292
293
// Process with modified features
294
// ... parsing logic ...
295
296
// Restore original features
297
parser.setFeatureMask(originalMask);
298
```
299
300
## Error Report Configuration
301
302
```java { .api }
303
public final class ErrorReportConfiguration implements Serializable {
304
public static final int DEFAULT_MAX_RAW_CONTENT_LENGTH = 500;
305
public static final int DEFAULT_MAX_ERROR_TOKEN_LENGTH = 256;
306
307
public static Builder builder();
308
public static ErrorReportConfiguration defaults();
309
310
public int getMaxRawContentLength();
311
public int getMaxErrorTokenLength();
312
313
public static class Builder {
314
public Builder maxRawContentLength(int maxRawContentLength);
315
public Builder maxErrorTokenLength(int maxErrorTokenLength);
316
public ErrorReportConfiguration build();
317
}
318
}
319
```
320
321
Example:
322
```java
323
JsonFactory factory = JsonFactory.builder()
324
.errorReportConfiguration(ErrorReportConfiguration.builder()
325
.maxErrorTokenLength(100)
326
.maxRawContentLength(200)
327
.build())
328
.build();
329
```