Skip to content

Commit 5239017

Browse files
authored
Implement new JsonFactory.builder().errorReportConfiguration() (#1072)
1 parent b38a0e4 commit 5239017

File tree

5 files changed

+262
-59
lines changed

5 files changed

+262
-59
lines changed

src/main/java/com/fasterxml/jackson/core/JsonFactory.java

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,14 @@ public static int collectDefaults() {
284284
*/
285285
protected StreamReadConstraints _streamReadConstraints;
286286

287+
/**
288+
* Container for configuration values used when handling errorneous token inputs.
289+
*
290+
* @see ErrorReportConfiguration
291+
* @since 2.16
292+
*/
293+
protected ErrorReportConfiguration _errorReportConfiguration;
294+
287295
/**
288296
* Write constraints to use for {@link JsonGenerator}s constructed using
289297
* this factory.
@@ -361,6 +369,7 @@ public JsonFactory(ObjectCodec oc) {
361369
_quoteChar = DEFAULT_QUOTE_CHAR;
362370
_streamReadConstraints = StreamReadConstraints.defaults();
363371
_streamWriteConstraints = StreamWriteConstraints.defaults();
372+
_errorReportConfiguration = ErrorReportConfiguration.defaults();
364373
_generatorDecorators = null;
365374
}
366375

@@ -385,6 +394,7 @@ protected JsonFactory(JsonFactory src, ObjectCodec codec)
385394
_generatorDecorators = _copy(src._generatorDecorators);
386395
_streamReadConstraints = Objects.requireNonNull(src._streamReadConstraints);
387396
_streamWriteConstraints = Objects.requireNonNull(src._streamWriteConstraints);
397+
_errorReportConfiguration = Objects.requireNonNull(src._errorReportConfiguration);
388398

389399
// JSON-specific
390400
_characterEscapes = src._characterEscapes;
@@ -412,6 +422,7 @@ public JsonFactory(JsonFactoryBuilder b) {
412422
_generatorDecorators = _copy(b._generatorDecorators);
413423
_streamReadConstraints = Objects.requireNonNull(b._streamReadConstraints);
414424
_streamWriteConstraints = Objects.requireNonNull(b._streamWriteConstraints);
425+
_errorReportConfiguration = Objects.requireNonNull(b._errorReportConfiguration);
415426

416427
// JSON-specific
417428
_characterEscapes = b._characterEscapes;
@@ -439,6 +450,7 @@ protected JsonFactory(TSFBuilder<?,?> b, boolean bogus) {
439450
_generatorDecorators = _copy(b._generatorDecorators);
440451
_streamReadConstraints = Objects.requireNonNull(b._streamReadConstraints);
441452
_streamWriteConstraints = Objects.requireNonNull(b._streamWriteConstraints);
453+
_errorReportConfiguration = Objects.requireNonNull(b._errorReportConfiguration);
442454

443455
// JSON-specific: need to assign even if not really used
444456
_characterEscapes = null;
@@ -838,6 +850,26 @@ public JsonFactory setStreamReadConstraints(StreamReadConstraints src) {
838850
return this;
839851
}
840852

853+
/**
854+
* Method for overriding {@link ErrorReportConfiguration} defined for
855+
* this factory.
856+
*<p>
857+
* NOTE: the preferred way to set constraints is by using
858+
* {@link JsonFactoryBuilder#errorReportConfiguration}: this method is only
859+
* provided to support older non-builder-based construction.
860+
* In Jackson 3.x this method will not be available.
861+
*
862+
* @param src Configuration
863+
*
864+
* @return This factory instance (to allow call chaining)
865+
*
866+
* @since 2.16
867+
*/
868+
public JsonFactory setErrorReportConfiguration(ErrorReportConfiguration src) {
869+
_errorReportConfiguration = Objects.requireNonNull(src, "Cannot pass null ErrorReportConfiguration");;
870+
return this;
871+
}
872+
841873
/**
842874
* Method for overriding {@link StreamWriteConstraints} defined for
843875
* this factory.
@@ -2112,7 +2144,7 @@ protected IOContext _createContext(ContentReference contentRef, boolean resource
21122144
if (contentRef == null) {
21132145
contentRef = ContentReference.unknown();
21142146
}
2115-
return new IOContext(_streamReadConstraints, _streamWriteConstraints,
2147+
return new IOContext(_streamReadConstraints, _streamWriteConstraints, _errorReportConfiguration,
21162148
_getBufferRecycler(), contentRef, resourceManaged);
21172149
}
21182150

@@ -2128,7 +2160,7 @@ protected IOContext _createContext(ContentReference contentRef, boolean resource
21282160
*/
21292161
@Deprecated // @since 2.13
21302162
protected IOContext _createContext(Object rawContentRef, boolean resourceManaged) {
2131-
return new IOContext(_streamReadConstraints, _streamWriteConstraints,
2163+
return new IOContext(_streamReadConstraints, _streamWriteConstraints, _errorReportConfiguration,
21322164
_getBufferRecycler(),
21332165
_createContentReference(rawContentRef),
21342166
resourceManaged);
@@ -2147,7 +2179,7 @@ protected IOContext _createContext(Object rawContentRef, boolean resourceManaged
21472179
protected IOContext _createNonBlockingContext(Object srcRef) {
21482180
// [jackson-core#479]: allow recycling for non-blocking parser again
21492181
// now that access is thread-safe
2150-
return new IOContext(_streamReadConstraints, _streamWriteConstraints,
2182+
return new IOContext(_streamReadConstraints, _streamWriteConstraints, _errorReportConfiguration,
21512183
_getBufferRecycler(),
21522184
_createContentReference(srcRef),
21532185
false);
@@ -2168,7 +2200,7 @@ protected IOContext _createNonBlockingContext(Object srcRef) {
21682200
protected ContentReference _createContentReference(Object contentAccessor) {
21692201
// 21-Mar-2021, tatu: For now assume "canHandleBinaryNatively()" is reliable
21702202
// indicator of textual vs binary format:
2171-
return ContentReference.construct(!canHandleBinaryNatively(), contentAccessor);
2203+
return ContentReference.construct(!canHandleBinaryNatively(), contentAccessor, _errorReportConfiguration);
21722204
}
21732205

21742206
/**
@@ -2192,7 +2224,7 @@ protected ContentReference _createContentReference(Object contentAccessor,
21922224
// 21-Mar-2021, tatu: For now assume "canHandleBinaryNatively()" is reliable
21932225
// indicator of textual vs binary format:
21942226
return ContentReference.construct(!canHandleBinaryNatively(),
2195-
contentAccessor, offset, length);
2227+
contentAccessor, offset, length, _errorReportConfiguration);
21962228
}
21972229

21982230
/*

src/main/java/com/fasterxml/jackson/core/JsonProcessingException.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*<p>
1515
* Since Jackson 2.12 extends intermediate {@link JacksonException} type
1616
* instead of directly extending {@link java.io.IOException}.
17+
*<p>
18+
* Since Jackson 2.16, handles its content as configured using {@link com.fasterxml.jackson.core.ErrorReportConfiguration}.
1719
*/
1820
public class JsonProcessingException extends JacksonException
1921
{

src/main/java/com/fasterxml/jackson/core/TSFBuilder.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ public abstract class TSFBuilder<F extends JsonFactory,
100100
*/
101101
protected List<JsonGeneratorDecorator> _generatorDecorators;
102102

103+
/**
104+
* Optional {@link ErrorReportConfiguration} to use.
105+
*
106+
* @since 2.16
107+
*/
108+
protected ErrorReportConfiguration _errorReportConfiguration;
109+
103110
/*
104111
/**********************************************************************
105112
/* Construction
@@ -120,6 +127,7 @@ protected TSFBuilder(JsonFactory base)
120127
_outputDecorator = base._outputDecorator;
121128
_streamReadConstraints = base._streamReadConstraints;
122129
_streamWriteConstraints = base._streamWriteConstraints;
130+
_errorReportConfiguration = base._errorReportConfiguration;
123131
_generatorDecorators = _copy(base._generatorDecorators);
124132
}
125133

@@ -134,6 +142,7 @@ protected TSFBuilder(int factoryFeatures,
134142
_outputDecorator = null;
135143
_streamReadConstraints = StreamReadConstraints.defaults();
136144
_streamWriteConstraints = StreamWriteConstraints.defaults();
145+
_errorReportConfiguration = ErrorReportConfiguration.defaults();
137146
_generatorDecorators = null;
138147
}
139148

@@ -324,6 +333,17 @@ public B streamReadConstraints(StreamReadConstraints streamReadConstraints) {
324333
return _this();
325334
}
326335

336+
/**
337+
* Sets the configuration for error tokens.
338+
*
339+
* @param errorReportConfiguration configuration values used for handling errorneous token inputs.
340+
* @return this factory
341+
* @since 2.16
342+
*/
343+
public B errorReportConfiguration(ErrorReportConfiguration errorReportConfiguration) {
344+
_errorReportConfiguration = errorReportConfiguration;
345+
return _this();
346+
}
327347
/**
328348
* Sets the constraints for streaming writes.
329349
*

src/test/java/com/fasterxml/jackson/core/ErrorReportConfigurationMaxRawContentLengthTest.java

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)