From d10ac6912f2e076f147a21ea198818a20e68442e Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Mon, 19 Jul 2021 19:40:24 -0700 Subject: [PATCH] Minor fixes to issue related to #700 (current name access via getText() should work) --- .../core/filter/FilteringParserDelegate.java | 60 ++++++++++++++++--- .../core/filter/ParserFiltering700Test.java | 15 +++++ 2 files changed, 67 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/core/filter/FilteringParserDelegate.java b/src/main/java/com/fasterxml/jackson/core/filter/FilteringParserDelegate.java index 808815b696..3540d8e9d0 100644 --- a/src/main/java/com/fasterxml/jackson/core/filter/FilteringParserDelegate.java +++ b/src/main/java/com/fasterxml/jackson/core/filter/FilteringParserDelegate.java @@ -865,11 +865,44 @@ public JsonParser skipChildren() throws IOException /********************************************************** */ - @Override public String getText() throws IOException { return delegate.getText(); } - @Override public boolean hasTextCharacters() { return delegate.hasTextCharacters(); } - @Override public char[] getTextCharacters() throws IOException { return delegate.getTextCharacters(); } - @Override public int getTextLength() throws IOException { return delegate.getTextLength(); } - @Override public int getTextOffset() throws IOException { return delegate.getTextOffset(); } + // 19-Jul-2021, tatu: Cannot quite just delegate these methods due to oddity + // of property name token, which may be buffered. + + @Override public String getText() throws IOException { + if (_currToken == JsonToken.FIELD_NAME) { + return currentName(); + } + return delegate.getText(); + } + + @Override public boolean hasTextCharacters() { + if (_currToken == JsonToken.FIELD_NAME) { + return false; + } + return delegate.hasTextCharacters(); + } + + @Override public char[] getTextCharacters() throws IOException { + // Not optimal but is correct, unlike delegating (as underlying stream + // may point to something else due to buffering) + if (_currToken == JsonToken.FIELD_NAME) { + return currentName().toCharArray(); + } + return delegate.getTextCharacters(); + } + + @Override public int getTextLength() throws IOException { + if (_currToken == JsonToken.FIELD_NAME) { + return currentName().length(); + } + return delegate.getTextLength(); + } + @Override public int getTextOffset() throws IOException { + if (_currToken == JsonToken.FIELD_NAME) { + return 0; + } + return delegate.getTextOffset(); + } /* /********************************************************** @@ -924,9 +957,20 @@ public JsonParser skipChildren() throws IOException @Override public double getValueAsDouble(double defaultValue) throws IOException { return delegate.getValueAsDouble(defaultValue); } @Override public boolean getValueAsBoolean() throws IOException { return delegate.getValueAsBoolean(); } @Override public boolean getValueAsBoolean(boolean defaultValue) throws IOException { return delegate.getValueAsBoolean(defaultValue); } - @Override public String getValueAsString() throws IOException { return delegate.getValueAsString(); } - @Override public String getValueAsString(String defaultValue) throws IOException { return delegate.getValueAsString(defaultValue); } - + + @Override public String getValueAsString() throws IOException { + if (_currToken == JsonToken.FIELD_NAME) { + return currentName(); + } + return delegate.getValueAsString(); + } + @Override public String getValueAsString(String defaultValue) throws IOException { + if (_currToken == JsonToken.FIELD_NAME) { + return currentName(); + } + return delegate.getValueAsString(defaultValue); + } + /* /********************************************************** /* Public API, access to token values, other diff --git a/src/test/java/com/fasterxml/jackson/core/filter/ParserFiltering700Test.java b/src/test/java/com/fasterxml/jackson/core/filter/ParserFiltering700Test.java index 55d61f9a3c..5ed65a130c 100644 --- a/src/test/java/com/fasterxml/jackson/core/filter/ParserFiltering700Test.java +++ b/src/test/java/com/fasterxml/jackson/core/filter/ParserFiltering700Test.java @@ -45,6 +45,10 @@ public void testSkippingRootLevel() throws Exception assertToken(JsonToken.FIELD_NAME, p.nextToken()); assertEquals("value", p.currentName()); + // 19-Jul-2021, tatu: while not ideal, existing contract is that "getText()" + // ought to return property name as well... + assertEquals("value", p.getText()); + assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); assertEquals(12, p.getIntValue()); @@ -70,10 +74,13 @@ public void testSkippingOneNested() throws Exception assertToken(JsonToken.FIELD_NAME, p.nextToken()); assertEquals("value", p.currentName()); + // as earlier, this needs to hold true too + assertEquals("value", p.getText()); assertToken(JsonToken.START_OBJECT, p.nextToken()); assertToken(JsonToken.FIELD_NAME, p.nextToken()); assertEquals("a", p.currentName()); + assertEquals("a", p.getText()); assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); assertEquals(12, p.getIntValue()); assertEquals(JsonToken.END_OBJECT, p.nextToken()); @@ -104,11 +111,16 @@ private void _testSkippingForSingleWithPath(boolean useNextName) throws Exceptio ); assertToken(JsonToken.START_OBJECT, p.nextToken()); + assertTrue(p.isExpectedStartObjectToken()); if (useNextName) { assertEquals("value", p.nextFieldName()); + // as earlier, this needs to hold true too + assertEquals("value", p.getText()); assertToken(JsonToken.START_OBJECT, p.nextToken()); + assertTrue(p.isExpectedStartObjectToken()); assertEquals("a", p.nextFieldName()); + assertEquals("a", p.getText()); assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); assertEquals(99, p.getIntValue()); assertNull(p.nextFieldName()); @@ -116,9 +128,12 @@ private void _testSkippingForSingleWithPath(boolean useNextName) throws Exceptio } else { assertToken(JsonToken.FIELD_NAME, p.nextToken()); assertEquals("value", p.currentName()); + assertEquals("value", p.getText()); assertToken(JsonToken.START_OBJECT, p.nextToken()); + assertTrue(p.isExpectedStartObjectToken()); assertToken(JsonToken.FIELD_NAME, p.nextToken()); assertEquals("a", p.currentName()); + assertEquals("a", p.getText()); assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); assertEquals(99, p.getIntValue()); assertEquals(JsonToken.END_OBJECT, p.nextToken());