Skip to content

Commit f45d035

Browse files
authored
Add appendIndex and appendProperty methods (#722)
1 parent f9f568a commit f45d035

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

src/main/java/com/fasterxml/jackson/core/JsonPointer.java

+58
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,64 @@ public JsonPointer append(JsonPointer tail) {
332332
return compile(currentJsonPointer + tail._asString);
333333
}
334334

335+
/**
336+
* ATTENTION! {@link JsonPointer} is head centric, tail appending is much costlier than head appending.
337+
* It is not recommended to overuse the method.
338+
*
339+
* Mutant factory method that will return
340+
*<ul>
341+
* <li>`this` instance if `property` is null or empty String, OR
342+
* </li>
343+
* <li>Newly constructed {@link JsonPointer} instance that starts with all segments
344+
* of `this`, followed by new segment of 'property' name.
345+
* </li>
346+
*</ul>
347+
*
348+
* 'property' format is starting separator (optional, added automatically if not provided) and new segment name.
349+
*
350+
* @param property new segment property name
351+
*
352+
* @return Either `this` instance, or a newly created combination, as per description above.
353+
*/
354+
public JsonPointer appendProperty(String property) {
355+
if (property == null || property.isEmpty()) {
356+
return this;
357+
}
358+
if (property.charAt(0) != SEPARATOR) {
359+
property = SEPARATOR + property;
360+
}
361+
String currentJsonPointer = _asString;
362+
if (currentJsonPointer.endsWith("/")) {
363+
//removes final slash
364+
currentJsonPointer = currentJsonPointer.substring(0, currentJsonPointer.length()-1);
365+
}
366+
return compile(currentJsonPointer + property);
367+
}
368+
369+
/**
370+
* ATTENTION! {@link JsonPointer} is head centric, tail appending is much costlier than head appending.
371+
* It is not recommended to overuse the method.
372+
*
373+
* Mutant factory method that will return newly constructed {@link JsonPointer} instance that starts with all
374+
* segments of `this`, followed by new segment of element 'index'. Element 'index' should be non-negative.
375+
*
376+
* @param index new segment element index
377+
*
378+
* @return Newly created combination, as per description above.
379+
* @throws IllegalArgumentException if element index is negative
380+
*/
381+
public JsonPointer appendIndex(int index) {
382+
if (index < 0) {
383+
throw new IllegalArgumentException("Negative index cannot be appended");
384+
}
385+
String currentJsonPointer = _asString;
386+
if (currentJsonPointer.endsWith("/")) {
387+
//removes final slash
388+
currentJsonPointer = currentJsonPointer.substring(0, currentJsonPointer.length()-1);
389+
}
390+
return compile(currentJsonPointer + SEPARATOR + index);
391+
}
392+
335393
/**
336394
* Method that may be called to see if the pointer head (first segment)
337395
* would match property (of a JSON Object) with given name.

src/test/java/com/fasterxml/jackson/core/TestJsonPointer.java

+25
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,31 @@ public void testAppendWithFinalSlash()
145145
assertEquals("extension", appended.last().getMatchingProperty());
146146
}
147147

148+
public void testAppendProperty()
149+
{
150+
final String INPUT = "/Image/15/name";
151+
final String APPEND_WITH_SLASH = "/extension";
152+
final String APPEND_NO_SLASH = "extension";
153+
154+
JsonPointer ptr = JsonPointer.compile(INPUT);
155+
JsonPointer appendedWithSlash = ptr.appendProperty(APPEND_WITH_SLASH);
156+
JsonPointer appendedNoSlash = ptr.appendProperty(APPEND_NO_SLASH);
157+
158+
assertEquals("extension", appendedWithSlash.last().getMatchingProperty());
159+
assertEquals("extension", appendedNoSlash.last().getMatchingProperty());
160+
}
161+
162+
public void testAppendIndex()
163+
{
164+
final String INPUT = "/Image/15/name";
165+
final int INDEX = 12;
166+
167+
JsonPointer ptr = JsonPointer.compile(INPUT);
168+
JsonPointer appended = ptr.appendIndex(INDEX);
169+
170+
assertEquals(12, appended.last().getMatchingIndex());
171+
}
172+
148173
public void testQuotedPath() throws Exception
149174
{
150175
final String INPUT = "/w~1out/til~0de/a~1b";

0 commit comments

Comments
 (0)