Skip to content

Commit

Permalink
Fixes stleary#410. Invalid processing of trailing / for JSON Pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
John J. Aylward committed Mar 19, 2018
1 parent 2a6b5ba commit edfbda0
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions JSONPointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,28 @@ public JSONPointer(final String pointer) {
throw new IllegalArgumentException("a JSON pointer should start with '/' or '#/'");
}
this.refTokens = new ArrayList<String>();
for (String token : refs.split("/")) {
this.refTokens.add(unescape(token));
}
int slashIdx = -1;
int prevSlashIdx = 0;
do {
prevSlashIdx = slashIdx + 1;
slashIdx = refs.indexOf('/', prevSlashIdx);
if(prevSlashIdx == slashIdx || prevSlashIdx == refs.length()) {
// found 2 slashes in a row ( obj//next )
// or single slash at the end of a string ( obj/test/ )
this.refTokens.add("");
} else if (slashIdx >= 0) {
final String token = refs.substring(prevSlashIdx, slashIdx);
this.refTokens.add(unescape(token));
} else {
// last item after separator, or no separator at all.
final String token = refs.substring(prevSlashIdx);
this.refTokens.add(unescape(token));
}
} while (slashIdx > 0);
// using split does not take into account consecutive separators or "ending nulls"
//for (String token : refs.split("/")) {
// this.refTokens.add(unescape(token));
//}
}

public JSONPointer(List<String> refTokens) {
Expand Down

0 comments on commit edfbda0

Please sign in to comment.