Skip to content

Commit bfb3008

Browse files
authored
Merge pull request stleary#411 from johnjaylward/JSONPointerTrailingSlash
Fix for invalid processing of trailing / for JSON Pointer
2 parents 06e9ad2 + 2362c93 commit bfb3008

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

JSONPointer.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,28 @@ public JSONPointer(final String pointer) {
158158
throw new IllegalArgumentException("a JSON pointer should start with '/' or '#/'");
159159
}
160160
this.refTokens = new ArrayList<String>();
161-
for (String token : refs.split("/")) {
162-
this.refTokens.add(unescape(token));
163-
}
161+
int slashIdx = -1;
162+
int prevSlashIdx = 0;
163+
do {
164+
prevSlashIdx = slashIdx + 1;
165+
slashIdx = refs.indexOf('/', prevSlashIdx);
166+
if(prevSlashIdx == slashIdx || prevSlashIdx == refs.length()) {
167+
// found 2 slashes in a row ( obj//next )
168+
// or single slash at the end of a string ( obj/test/ )
169+
this.refTokens.add("");
170+
} else if (slashIdx >= 0) {
171+
final String token = refs.substring(prevSlashIdx, slashIdx);
172+
this.refTokens.add(unescape(token));
173+
} else {
174+
// last item after separator, or no separator at all.
175+
final String token = refs.substring(prevSlashIdx);
176+
this.refTokens.add(unescape(token));
177+
}
178+
} while (slashIdx >= 0);
179+
// using split does not take into account consecutive separators or "ending nulls"
180+
//for (String token : refs.split("/")) {
181+
// this.refTokens.add(unescape(token));
182+
//}
164183
}
165184

166185
public JSONPointer(List<String> refTokens) {

0 commit comments

Comments
 (0)