Skip to content

Commit

Permalink
Final fixes to JsonPointer
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 7, 2022
1 parent 1a5f364 commit a09cbd0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
12 changes: 8 additions & 4 deletions src/main/java/com/fasterxml/jackson/core/JsonPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ protected static JsonPointer _parseTail(final String fullPath)
// 04-Oct-2022, tatu: Let's decode escaped segment
// instead of recursive call
StringBuilder sb = new StringBuilder(32);
i = _extractEscapedSegment(fullPath, i, sb);
i = _extractEscapedSegment(fullPath, startOffset+1, i, sb);
final String segment = sb.toString();
if (i < 0) { // end!
return _buildPath(fullPath, startOffset, segment, parent);
Expand Down Expand Up @@ -691,18 +691,22 @@ private static JsonPointer _buildPath(final String fullPath, int fullPathOffset,
* within segment.
*
* @param input Full input for the tail being parsed
* @param firstCharOffset Offset of the first character of segment (one
* after slash)
* @param i Offset to character after tilde
* @param sb StringBuilder into which unquoted segment is added
*
* @return Offset at which slash was encountered, if any, or -1
* if expression ended without seeing unescaped slash
*/
protected static int _extractEscapedSegment(String input, int i,
protected static int _extractEscapedSegment(String input, int firstCharOffset,
int i,
StringBuilder sb)
{
final int end = input.length();
if (i > 2) {
sb.append(input, 1, i-1);
final int toCopy = i - 1 - firstCharOffset;
if (toCopy > 0) {
sb.append(input, firstCharOffset, i-1);
}
_appendEscape(sb, input.charAt(i++));
while (i < end) {
Expand Down
16 changes: 12 additions & 4 deletions src/test/java/com/fasterxml/jackson/core/JsonPointerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ public void testEquality() {
assertFalse(JsonPointer.empty().equals(JsonPointer.compile("/12")));
assertFalse(JsonPointer.compile("/12").equals(JsonPointer.empty()));

assertEquals(JsonPointer.compile("/a/b/c").tail(),
JsonPointer.compile("/foo/b/c").tail());

JsonPointer abcDef = JsonPointer.compile("/abc/def");
JsonPointer def = JsonPointer.compile("/def");
assertEquals(abcDef.tail(), def);
assertEquals(def, abcDef.tail());

// expr != String
assertFalse(JsonPointer.empty().equals("/"));
}
Expand Down Expand Up @@ -206,7 +214,7 @@ public void testAppendIndex()

public void testQuotedPath() throws Exception
{
final String INPUT = "/w~1out/til~0de/a~1b";
final String INPUT = "/w~1out/til~0de/~1ab";

JsonPointer ptr = JsonPointer.compile(INPUT);
assertFalse(ptr.matches());
Expand All @@ -221,14 +229,14 @@ public void testQuotedPath() throws Exception
assertEquals(-1, ptr.getMatchingIndex());
assertEquals("til~de", ptr.getMatchingProperty());
assertEquals("/til~0de", ptr.head().toString());
assertEquals("/til~0de/a~1b", ptr.toString());
assertEquals("/til~0de/~1ab", ptr.toString());

ptr = ptr.tail();
assertNotNull(ptr);
assertFalse(ptr.matches());
assertEquals(-1, ptr.getMatchingIndex());
assertEquals("a/b", ptr.getMatchingProperty());
assertEquals("/a~1b", ptr.toString());
assertEquals("/ab", ptr.getMatchingProperty());
assertEquals("/~1ab", ptr.toString());
assertEquals("", ptr.head().toString());

// done!
Expand Down

0 comments on commit a09cbd0

Please sign in to comment.