You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@johnzon.apache.org by rm...@apache.org on 2020/03/27 07:28:01 UTC
[johnzon] 01/02: Added possibility to use minus as array index for
the remove operation.
This is an automated email from the ASF dual-hosted git repository.
rmannibucau pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/johnzon.git
commit 2511babf3fc4e272a40183e9e5606009231ee6d5
Author: Markus Paeschke <ma...@gmail.com>
AuthorDate: Fri Mar 20 10:58:44 2020 +0100
Added possibility to use minus as array index for the remove operation.
---
.../main/java/org/apache/johnzon/core/JsonPointerImpl.java | 7 ++++---
.../test/java/org/apache/johnzon/core/JsonPointerTest.java | 13 +++++++++++++
2 files changed, 17 insertions(+), 3 deletions(-)
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonPointerImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonPointerImpl.java
index 89eed57..46d62b3 100644
--- a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonPointerImpl.java
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonPointerImpl.java
@@ -339,8 +339,7 @@ public class JsonPointerImpl implements JsonPointer {
try {
JsonArray jsonArray = (JsonArray) jsonValue;
- int arrayIndex = Integer.parseInt(referenceToken);
- validateArraySize(jsonArray, arrayIndex, jsonArray.size());
+ int arrayIndex = getArrayIndex(referenceToken, jsonArray, false);
return jsonArray.get(arrayIndex);
} catch (NumberFormatException e) {
throw new JsonException("'" + referenceToken + "' is no valid array index", e);
@@ -460,6 +459,8 @@ public class JsonPointerImpl implements JsonPointer {
private int getArrayIndex(String referenceToken, JsonArray jsonArray, boolean addOperation) {
if (addOperation && referenceToken.equals("-")) {
return jsonArray.size();
+ } else if (!addOperation && referenceToken.equals("-")) {
+ return jsonArray.size() - 1;
}
validateArrayIndex(referenceToken);
@@ -486,7 +487,7 @@ public class JsonPointerImpl implements JsonPointer {
}
private void validateArrayIndex(String referenceToken) throws JsonException {
- if (referenceToken.startsWith("+") || referenceToken.startsWith("-")) {
+ if (referenceToken.startsWith("+") || (referenceToken.startsWith("-") && referenceToken.length() > 1)) {
throw new JsonException("An array index must not start with '" + referenceToken.charAt(0) + "'");
}
if (referenceToken.startsWith("0") && referenceToken.length() > 1) {
diff --git a/johnzon-core/src/test/java/org/apache/johnzon/core/JsonPointerTest.java b/johnzon-core/src/test/java/org/apache/johnzon/core/JsonPointerTest.java
index b07bb6d..f2b7164 100644
--- a/johnzon-core/src/test/java/org/apache/johnzon/core/JsonPointerTest.java
+++ b/johnzon-core/src/test/java/org/apache/johnzon/core/JsonPointerTest.java
@@ -457,6 +457,19 @@ public class JsonPointerTest {
}
@Test
+ public void testRemoveLastArrayElement() {
+ JsonPointerImpl jsonPointer = new JsonPointerImpl(JsonProvider.provider(), "/0/-");
+ JsonStructure target = Json.createArrayBuilder()
+ .add(Json.createArrayBuilder()
+ .add("bar")
+ .add("qux")
+ .add("baz")).build(); // [["bar","qux","baz"]]
+
+ JsonStructure result = jsonPointer.remove(target);
+ assertEquals("[[\"bar\",\"qux\"]]", result.toString()); // [["bar","baz"]]
+ }
+
+ @Test
public void testRemoveObjectMember() {
JsonPointerImpl jsonPointer = new JsonPointerImpl(JsonProvider.provider(), "/baz");
JsonStructure target = Json.createObjectBuilder()