From dfa3b11bf64cfe270c56affef8c27527c59142a2 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Wed, 27 Jan 2021 19:31:56 -0800 Subject: [PATCH] Backport #671 --- release-notes/VERSION-2.x | 2 + .../fasterxml/jackson/core/JsonParser.java | 48 ++++++++-- .../jackson/core/util/JsonParserDelegate.java | 93 +++++++++++-------- .../core/json/async/AsyncLocationTest.java | 2 +- 4 files changed, 95 insertions(+), 50 deletions(-) diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 3740c289b7..8a19f86fb8 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -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) diff --git a/src/main/java/com/fasterxml/jackson/core/JsonParser.java b/src/main/java/com/fasterxml/jackson/core/JsonParser.java index 0e3c45ed3b..9cddc4f37d 100644 --- a/src/main/java/com/fasterxml/jackson/core/JsonParser.java +++ b/src/main/java/com/fasterxml/jackson/core/JsonParser.java @@ -682,35 +682,63 @@ public JacksonFeatureSet getReadCapabilities() { public abstract JsonStreamContext getParsingContext(); /** - * Method that return the starting 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. *

* 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 starting 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. *

* 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 diff --git a/src/main/java/com/fasterxml/jackson/core/util/JsonParserDelegate.java b/src/main/java/com/fasterxml/jackson/core/util/JsonParserDelegate.java index 602c5b8518..8ec8f629c9 100644 --- a/src/main/java/com/fasterxml/jackson/core/util/JsonParserDelegate.java +++ b/src/main/java/com/fasterxml/jackson/core/util/JsonParserDelegate.java @@ -37,9 +37,9 @@ public void setCurrentValue(Object v) { } /* - /********************************************************** + /********************************************************************** /* Public API, configuration - /********************************************************** + /********************************************************************** */ @Override public void setCodec(ObjectCodec c) { delegate.setCodec(c); } @@ -86,9 +86,9 @@ public JsonParser overrideFormatFeatures(int values, int mask) { @Override public Object getInputSource() { return delegate.getInputSource(); } /* - /********************************************************** + /********************************************************************** /* Capability introspection - /********************************************************** + /********************************************************************** */ @Override public boolean requiresCustomCodec() { return delegate.requiresCustomCodec(); } @@ -96,35 +96,61 @@ public JsonParser overrideFormatFeatures(int values, int mask) { @Override public JacksonFeatureSet 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(); } @@ -132,19 +158,9 @@ public JsonParser overrideFormatFeatures(int values, int mask) { @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(); } @@ -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 @@ -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(); } @@ -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(); } @@ -238,9 +253,9 @@ public JsonParser skipChildren() throws IOException { } /* - /********************************************************** + /********************************************************************** /* Public API, Native Ids (type, object) - /********************************************************** + /********************************************************************** */ @Override public boolean canReadObjectId() { return delegate.canReadObjectId(); } @@ -249,9 +264,9 @@ public JsonParser skipChildren() throws IOException { @Override public Object getTypeId() throws IOException { return delegate.getTypeId(); } /* - /********************************************************** + /********************************************************************** /* Extended API - /********************************************************** + /********************************************************************** */ /** diff --git a/src/test/java/com/fasterxml/jackson/core/json/async/AsyncLocationTest.java b/src/test/java/com/fasterxml/jackson/core/json/async/AsyncLocationTest.java index e108b98e11..d5035d1dc1 100644 --- a/src/test/java/com/fasterxml/jackson/core/json/async/AsyncLocationTest.java +++ b/src/test/java/com/fasterxml/jackson/core/json/async/AsyncLocationTest.java @@ -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());