Skip to content

Commit

Permalink
Retrofit IOContext with ErrorReportConfiguration (#1068)
Browse files Browse the repository at this point in the history
  • Loading branch information
JooHyukKim authored Aug 1, 2023
1 parent 55f47b7 commit 1f7cef9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ public abstract class ParserMinimalBase extends JsonParser
* as part of error messages.
*
* @since 2.9
* @deprecated Since 2.16. {@link ErrorReportConfiguration#getMaxErrorTokenLength()} will be used instead.
*/
@Deprecated
protected final static int MAX_ERROR_TOKEN_LENGTH = 256;

/*
Expand Down
55 changes: 43 additions & 12 deletions src/main/java/com/fasterxml/jackson/core/io/IOContext.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fasterxml.jackson.core.io;

import com.fasterxml.jackson.core.ErrorReportConfiguration;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.StreamReadConstraints;
import com.fasterxml.jackson.core.StreamWriteConstraints;
Expand Down Expand Up @@ -68,6 +69,11 @@ public class IOContext
*/
protected final StreamReadConstraints _streamReadConstraints;

/**
* @since 2.16
*/
protected final ErrorReportConfiguration _errorReportConfiguration;

/**
* @since 2.16
*/
Expand Down Expand Up @@ -129,18 +135,38 @@ public class IOContext
* @param managedResource Whether input source is managed (owned) by Jackson library
*
* @since 2.16
* @deprecated Since 2.16, use {@link #IOContext(StreamReadConstraints, StreamWriteConstraints, BufferRecycler,
* ContentReference, boolean, ErrorReportConfiguration)} instead.
*/
@Deprecated
public IOContext(StreamReadConstraints src, StreamWriteConstraints swc, BufferRecycler br,
ContentReference contentRef, boolean managedResource)
{
_streamReadConstraints = (src == null) ?
StreamReadConstraints.defaults() : src;
_streamWriteConstraints = (swc == null) ?
StreamWriteConstraints.defaults() : swc;
this(src, swc, br, contentRef, managedResource, ErrorReportConfiguration.defaults());
}

/**
* Main constructor to use.
*
* @param src constraints for streaming reads
* @param swc constraints for streaming writes
* @param br BufferRecycler to use, if any ({@code null} if none)
* @param contentRef Input source reference for location reporting
* @param managedResource Whether input source is managed (owned) by Jackson library
* @param erc Error report configuration to use
*
* @since 2.16
*/
public IOContext(StreamReadConstraints src, StreamWriteConstraints swc, BufferRecycler br,
ContentReference contentRef, boolean managedResource, ErrorReportConfiguration erc)
{
_streamReadConstraints = src;
_streamWriteConstraints = swc;
_bufferRecycler = br;
_contentReference = contentRef;
_sourceRef = contentRef.getRawContent();
_managedResource = managedResource;
_errorReportConfiguration = erc;
}

/**
Expand All @@ -150,19 +176,14 @@ public IOContext(StreamReadConstraints src, StreamWriteConstraints swc, BufferRe
* @param managedResource Whether input source is managed (owned) by Jackson library
*
* @since 2.15
* @deprecated use v2.16 constructor with additional <code>StreamWriteConstraints</code>
* @deprecated Since 2.16. Use {@link #IOContext(StreamReadConstraints, StreamWriteConstraints, BufferRecycler,
* ContentReference, boolean, ErrorReportConfiguration)} instead.
*/
@Deprecated
public IOContext(StreamReadConstraints src, BufferRecycler br,
ContentReference contentRef, boolean managedResource)
{
_streamReadConstraints = (src == null) ?
StreamReadConstraints.defaults() : src;
_streamWriteConstraints = StreamWriteConstraints.defaults();
_bufferRecycler = br;
_contentReference = contentRef;
_sourceRef = contentRef.getRawContent();
_managedResource = managedResource;
this(src, StreamWriteConstraints.defaults(), br, contentRef, managedResource, ErrorReportConfiguration.defaults());
}

/**
Expand Down Expand Up @@ -199,6 +220,16 @@ public StreamWriteConstraints streamWriteConstraints() {
return _streamWriteConstraints;
}

/**
* @return Configured {@link ErrorReportConfiguration}, containing configured values for
* handling error reporting.
*
* @since 2.16
*/
public ErrorReportConfiguration errorReportConfiguration() {
return _errorReportConfiguration;
}

public void setEncoding(JsonEncoding enc) {
_encoding = enc;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3033,7 +3033,7 @@ protected void _reportInvalidToken(String matchedPart, String msg) throws IOExce
}
++_inputPtr;
sb.append(c);
if (sb.length() >= MAX_ERROR_TOKEN_LENGTH) {
if (sb.length() >= _ioContext.errorReportConfiguration().getMaxErrorTokenLength()) {
sb.append("...");
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3691,7 +3691,7 @@ protected void _reportInvalidToken(String matchedPart, String msg) throws IOExce
break;
}
sb.append(c);
if (sb.length() >= MAX_ERROR_TOKEN_LENGTH) {
if (sb.length() >= _ioContext.errorReportConfiguration().getMaxErrorTokenLength()) {
sb.append("...");
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1244,7 +1244,7 @@ protected JsonToken _finishErrorToken() throws IOException
// 11-Jan-2016, tatu: note: we will fully consume the character,
// included or not, so if recovery was possible, it'd be off-by-one...
_textBuffer.append(ch);
if (_textBuffer.size() < MAX_ERROR_TOKEN_LENGTH) {
if (_textBuffer.size() < _ioContext.errorReportConfiguration().getMaxErrorTokenLength()) {
continue;
}
}
Expand Down

0 comments on commit 1f7cef9

Please sign in to comment.