Skip to content

Commit

Permalink
Backport #671
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 28, 2021
1 parent 99bf0cf commit dfa3b11
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 50 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ JSON library.
2.13.0 (not yet released)

#664: Add `StreamWriteException` type to eventually replace `JsonGenerationException`
#671: Add `getCurrentLocation()`/`getTokenLocation()` to replace
`currentLocation()`/`currentTokenLocation()`

2.12.1 (08-Jan-2021)

Expand Down
48 changes: 38 additions & 10 deletions src/main/java/com/fasterxml/jackson/core/JsonParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -682,35 +682,63 @@ public JacksonFeatureSet<StreamReadCapability> getReadCapabilities() {
public abstract JsonStreamContext getParsingContext();

/**
* Method that return the <b>starting</b> location of the current
* token; that is, position of the first character from input
* that starts the current token.
* Method that returns location of the last processed input unit (character
* or byte) from the input;
* usually for error reporting purposes.
*<p>
* Note that the location is not guaranteed to be accurate (although most
* implementation will try their best): some implementations may only
* return {@link JsonLocation#NA} due to not having access
* report specific boundary locations (start or end locations of tokens)
* and others only return {@link JsonLocation#NA} due to not having access
* to input location information (when delegating actual decoding work
* to other library)
*
* @return Starting location of the token parser currently points to
* @return Location of the last processed input unit (byte or character)
*
* @since 2.13
*/
public abstract JsonLocation getTokenLocation();
public JsonLocation currentLocation() {
return getCurrentLocation();
}

/**
* Method that returns location of the last processed character;
* usually for error reporting purposes.
* Method that return the <b>starting</b> location of the current
* (most recently returned)
* token; that is, the position of the first input unit (character or byte) from input
* that starts the current token.
*<p>
* Note that the location is not guaranteed to be accurate (although most
* implementation will try their best): some implementations may only
* report specific boundary locations (start or end locations of tokens)
* and others only return {@link JsonLocation#NA} due to not having access
* return {@link JsonLocation#NA} due to not having access
* to input location information (when delegating actual decoding work
* to other library)
*
* @return Starting location of the token parser currently points to
*
* @since 2.13 (will eventually replace {@link #getTokenLocation})
*/
public JsonLocation currentTokenLocation() {
return getTokenLocation();
}

// TODO: deprecate in 2.14 or later
/**
* Alias for {@link #currentLocation()}, to be deprecated in later
* Jackson 2.x versions (and removed from Jackson 3.0).
*
* @return Location of the last processed input unit (byte or character)
*/
public abstract JsonLocation getCurrentLocation();

// TODO: deprecate in 2.14 or later
/**
* Alias for {@link #currentTokenLocation()}, to be deprecated in later
* Jackson 2.x versions (and removed from Jackson 3.0).
*
* @return Starting location of the token parser currently points to
*/
public abstract JsonLocation getTokenLocation();

/*
/**********************************************************
/* Buffer handling
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public void setCurrentValue(Object v) {
}

/*
/**********************************************************
/**********************************************************************
/* Public API, configuration
/**********************************************************
/**********************************************************************
*/

@Override public void setCodec(ObjectCodec c) { delegate.setCodec(c); }
Expand Down Expand Up @@ -86,65 +86,81 @@ public JsonParser overrideFormatFeatures(int values, int mask) {
@Override public Object getInputSource() { return delegate.getInputSource(); }

/*
/**********************************************************
/**********************************************************************
/* Capability introspection
/**********************************************************
/**********************************************************************
*/

@Override public boolean requiresCustomCodec() { return delegate.requiresCustomCodec(); }

@Override public JacksonFeatureSet<StreamReadCapability> getReadCapabilities() { return delegate.getReadCapabilities(); }

/*
/**********************************************************
/**********************************************************************
/* Closeable impl
/**********************************************************
/**********************************************************************
*/

@Override public void close() throws IOException { delegate.close(); }
@Override public boolean isClosed() { return delegate.isClosed(); }

/*
/**********************************************************
/* Public API, token accessors
/**********************************************************
/**********************************************************************
/* Public API, state override methods
/**********************************************************************
*/

@Override public void clearCurrentToken() { delegate.clearCurrentToken(); }
@Override public JsonToken getLastClearedToken() { return delegate.getLastClearedToken(); }
@Override public void overrideCurrentName(String name) { delegate.overrideCurrentName(name); }

/*
/**********************************************************************
/* Public API, state/location accessors
/**********************************************************************
*/

@Override public JsonStreamContext getParsingContext() { return delegate.getParsingContext(); }

@Override public JsonToken currentToken() { return delegate.currentToken(); }
@Override public int currentTokenId() { return delegate.currentTokenId(); }

@Override public JsonToken getCurrentToken() { return delegate.getCurrentToken(); }
@Override public String currentName() throws IOException { return delegate.currentName(); }

@Override public JsonLocation currentLocation() { return delegate.getCurrentLocation(); }
@Override public JsonLocation currentTokenLocation() { return delegate.getTokenLocation(); }

// TODO: deprecate in 2.14 or later
@Override public JsonToken getCurrentToken() { return delegate.getCurrentToken(); }
@Deprecated // since 2.12
@Override public int getCurrentTokenId() { return delegate.getCurrentTokenId(); }
// TODO: deprecate in 2.14 or later
@Override public String getCurrentName() throws IOException { return delegate.getCurrentName(); }

// TODO: deprecate in 2.14 or later
@Override public JsonLocation getCurrentLocation() { return delegate.getCurrentLocation(); }
// TODO: deprecate in 2.14 or later
@Override public JsonLocation getTokenLocation() { return delegate.getTokenLocation(); }

/*
/**********************************************************************
/* Public API, token accessors
/**********************************************************************
*/

@Override public boolean hasCurrentToken() { return delegate.hasCurrentToken(); }
@Override public boolean hasTokenId(int id) { return delegate.hasTokenId(id); }
@Override public boolean hasToken(JsonToken t) { return delegate.hasToken(t); }

@Override public String getCurrentName() throws IOException { return delegate.getCurrentName(); }
@Override public JsonLocation getCurrentLocation() { return delegate.getCurrentLocation(); }
@Override public JsonStreamContext getParsingContext() { return delegate.getParsingContext(); }
@Override public boolean isExpectedStartArrayToken() { return delegate.isExpectedStartArrayToken(); }
@Override public boolean isExpectedStartObjectToken() { return delegate.isExpectedStartObjectToken(); }
@Override public boolean isExpectedNumberIntToken() { return delegate.isExpectedNumberIntToken(); }

@Override public boolean isNaN() throws IOException { return delegate.isNaN(); }

/*
/**********************************************************
/* Public API, token state overrides
/**********************************************************
*/

@Override public void clearCurrentToken() { delegate.clearCurrentToken(); }
@Override public JsonToken getLastClearedToken() { return delegate.getLastClearedToken(); }
@Override public void overrideCurrentName(String name) { delegate.overrideCurrentName(name); }

/*
/**********************************************************
/* Public API, access to token information, text
/**********************************************************
/**********************************************************************
/* Public API, access to token textual content
/**********************************************************************
*/

@Override public String getText() throws IOException { return delegate.getText(); }
Expand All @@ -155,9 +171,9 @@ public JsonParser overrideFormatFeatures(int values, int mask) {
@Override public int getText(Writer writer) throws IOException, UnsupportedOperationException { return delegate.getText(writer); }

/*
/**********************************************************
/* Public API, access to token information, numeric
/**********************************************************
/**********************************************************************
/* Public API, access to token numeric values
/**********************************************************************
*/

@Override
Expand Down Expand Up @@ -197,9 +213,9 @@ public JsonParser overrideFormatFeatures(int values, int mask) {
public Number getNumberValueExact() throws IOException { return delegate.getNumberValueExact(); }

/*
/**********************************************************
/**********************************************************************
/* Public API, access to token information, coercion/conversion
/**********************************************************
/**********************************************************************
*/

@Override public int getValueAsInt() throws IOException { return delegate.getValueAsInt(); }
Expand All @@ -214,15 +230,14 @@ public JsonParser overrideFormatFeatures(int values, int mask) {
@Override public String getValueAsString(String defaultValue) throws IOException { return delegate.getValueAsString(defaultValue); }

/*
/**********************************************************
/**********************************************************************
/* Public API, access to token values, other
/**********************************************************
/**********************************************************************
*/

@Override public Object getEmbeddedObject() throws IOException { return delegate.getEmbeddedObject(); }
@Override public byte[] getBinaryValue(Base64Variant b64variant) throws IOException { return delegate.getBinaryValue(b64variant); }
@Override public int readBinaryValue(Base64Variant b64variant, OutputStream out) throws IOException { return delegate.readBinaryValue(b64variant, out); }
@Override public JsonLocation getTokenLocation() { return delegate.getTokenLocation(); }

@Override public JsonToken nextToken() throws IOException { return delegate.nextToken(); }

Expand All @@ -238,9 +253,9 @@ public JsonParser skipChildren() throws IOException {
}

/*
/**********************************************************
/**********************************************************************
/* Public API, Native Ids (type, object)
/**********************************************************
/**********************************************************************
*/

@Override public boolean canReadObjectId() { return delegate.canReadObjectId(); }
Expand All @@ -249,9 +264,9 @@ public JsonParser skipChildren() throws IOException {
@Override public Object getTypeId() throws IOException { return delegate.getTypeId(); }

/*
/**********************************************************
/**********************************************************************
/* Extended API
/**********************************************************
/**********************************************************************
*/

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void testLocationOffsets() throws Exception

feeder.feedInput(input, 2, 3);
assertEquals(JsonToken.START_ARRAY, parser.nextToken());
assertEquals(1, parser.getCurrentLocation().getByteOffset());
assertEquals(1, parser.currentLocation().getByteOffset());
assertEquals(1, parser.getTokenLocation().getByteOffset());
assertEquals(1, parser.getCurrentLocation().getLineNr());
assertEquals(1, parser.getTokenLocation().getLineNr());
Expand Down

0 comments on commit dfa3b11

Please sign in to comment.