Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement new JsonFactory.builder().errorReportConfiguration() #1072

Merged
merged 6 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions src/main/java/com/fasterxml/jackson/core/JsonFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,14 @@ public static int collectDefaults() {
*/
protected StreamReadConstraints _streamReadConstraints;

/**
* Container for configuration values used when handling errorneous token inputs.
*
* @see ErrorReportConfiguration
* @since 2.16
*/
protected ErrorReportConfiguration _errorReportConfiguration;

/**
* Write constraints to use for {@link JsonGenerator}s constructed using
* this factory.
Expand Down Expand Up @@ -361,6 +369,7 @@ public JsonFactory(ObjectCodec oc) {
_quoteChar = DEFAULT_QUOTE_CHAR;
_streamReadConstraints = StreamReadConstraints.defaults();
_streamWriteConstraints = StreamWriteConstraints.defaults();
_errorReportConfiguration = ErrorReportConfiguration.defaults();
_generatorDecorators = null;
}

Expand All @@ -385,6 +394,7 @@ protected JsonFactory(JsonFactory src, ObjectCodec codec)
_generatorDecorators = _copy(src._generatorDecorators);
_streamReadConstraints = Objects.requireNonNull(src._streamReadConstraints);
_streamWriteConstraints = Objects.requireNonNull(src._streamWriteConstraints);
_errorReportConfiguration = Objects.requireNonNull(src._errorReportConfiguration);

// JSON-specific
_characterEscapes = src._characterEscapes;
Expand Down Expand Up @@ -412,6 +422,7 @@ public JsonFactory(JsonFactoryBuilder b) {
_generatorDecorators = _copy(b._generatorDecorators);
_streamReadConstraints = Objects.requireNonNull(b._streamReadConstraints);
_streamWriteConstraints = Objects.requireNonNull(b._streamWriteConstraints);
_errorReportConfiguration = Objects.requireNonNull(b._errorReportConfiguration);

// JSON-specific
_characterEscapes = b._characterEscapes;
Expand Down Expand Up @@ -439,6 +450,7 @@ protected JsonFactory(TSFBuilder<?,?> b, boolean bogus) {
_generatorDecorators = _copy(b._generatorDecorators);
_streamReadConstraints = Objects.requireNonNull(b._streamReadConstraints);
_streamWriteConstraints = Objects.requireNonNull(b._streamWriteConstraints);
_errorReportConfiguration = Objects.requireNonNull(b._errorReportConfiguration);

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

/**
* Method for overriding {@link ErrorReportConfiguration} defined for
* this factory.
*<p>
* NOTE: the preferred way to set constraints is by using
* {@link JsonFactoryBuilder#errorReportConfiguration}: this method is only
* provided to support older non-builder-based construction.
* In Jackson 3.x this method will not be available.
*
* @param src Configuration
*
* @return This factory instance (to allow call chaining)
*
* @since 2.16
*/
public JsonFactory setErrorReportConfiguration(ErrorReportConfiguration src) {
_errorReportConfiguration = Objects.requireNonNull(src, "Cannot pass null ErrorReportConfiguration");;
return this;
}

/**
* Method for overriding {@link StreamWriteConstraints} defined for
* this factory.
Expand Down Expand Up @@ -2112,7 +2144,7 @@ protected IOContext _createContext(ContentReference contentRef, boolean resource
if (contentRef == null) {
contentRef = ContentReference.unknown();
}
return new IOContext(_streamReadConstraints, _streamWriteConstraints,
return new IOContext(_streamReadConstraints, _streamWriteConstraints, _errorReportConfiguration,
_getBufferRecycler(), contentRef, resourceManaged);
}

Expand All @@ -2128,7 +2160,7 @@ protected IOContext _createContext(ContentReference contentRef, boolean resource
*/
@Deprecated // @since 2.13
protected IOContext _createContext(Object rawContentRef, boolean resourceManaged) {
return new IOContext(_streamReadConstraints, _streamWriteConstraints,
return new IOContext(_streamReadConstraints, _streamWriteConstraints, _errorReportConfiguration,
_getBufferRecycler(),
_createContentReference(rawContentRef),
resourceManaged);
Expand All @@ -2147,7 +2179,7 @@ protected IOContext _createContext(Object rawContentRef, boolean resourceManaged
protected IOContext _createNonBlockingContext(Object srcRef) {
// [jackson-core#479]: allow recycling for non-blocking parser again
// now that access is thread-safe
return new IOContext(_streamReadConstraints, _streamWriteConstraints,
return new IOContext(_streamReadConstraints, _streamWriteConstraints, _errorReportConfiguration,
_getBufferRecycler(),
_createContentReference(srcRef),
false);
Expand All @@ -2168,7 +2200,7 @@ protected IOContext _createNonBlockingContext(Object srcRef) {
protected ContentReference _createContentReference(Object contentAccessor) {
// 21-Mar-2021, tatu: For now assume "canHandleBinaryNatively()" is reliable
// indicator of textual vs binary format:
return ContentReference.construct(!canHandleBinaryNatively(), contentAccessor);
return ContentReference.construct(!canHandleBinaryNatively(), contentAccessor, _errorReportConfiguration);
}

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

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*<p>
* Since Jackson 2.12 extends intermediate {@link JacksonException} type
* instead of directly extending {@link java.io.IOException}.
*<p>
* Since Jackson 2.16, handles its content as configured using {@link com.fasterxml.jackson.core.ErrorReportConfiguration}.
*/
public class JsonProcessingException extends JacksonException
{
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/fasterxml/jackson/core/TSFBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ public abstract class TSFBuilder<F extends JsonFactory,
*/
protected List<JsonGeneratorDecorator> _generatorDecorators;

/**
* Optional {@link ErrorReportConfiguration} to use.
*
* @since 2.16
*/
protected ErrorReportConfiguration _errorReportConfiguration;

/*
/**********************************************************************
/* Construction
Expand All @@ -120,6 +127,7 @@ protected TSFBuilder(JsonFactory base)
_outputDecorator = base._outputDecorator;
_streamReadConstraints = base._streamReadConstraints;
_streamWriteConstraints = base._streamWriteConstraints;
_errorReportConfiguration = base._errorReportConfiguration;
_generatorDecorators = _copy(base._generatorDecorators);
}

Expand All @@ -134,6 +142,7 @@ protected TSFBuilder(int factoryFeatures,
_outputDecorator = null;
_streamReadConstraints = StreamReadConstraints.defaults();
_streamWriteConstraints = StreamWriteConstraints.defaults();
_errorReportConfiguration = ErrorReportConfiguration.defaults();
_generatorDecorators = null;
}

Expand Down Expand Up @@ -324,6 +333,17 @@ public B streamReadConstraints(StreamReadConstraints streamReadConstraints) {
return _this();
}

/**
* Sets the configuration for error tokens.
*
* @param errorReportConfiguration configuration values used for handling errorneous token inputs.
* @return this factory
* @since 2.16
*/
public B errorReportConfiguration(ErrorReportConfiguration errorReportConfiguration) {
_errorReportConfiguration = errorReportConfiguration;
return _this();
}
/**
* Sets the constraints for streaming writes.
*
Expand Down

This file was deleted.

Loading