Skip to content

Commit

Permalink
JSONPath add static method 'remove', JSONPath.set & JSONPath.remove s…
Browse files Browse the repository at this point in the history
…upport more syntax
  • Loading branch information
wenshao committed May 4, 2022
1 parent 7f4cfc8 commit f5057e6
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 1 deletion.
105 changes: 104 additions & 1 deletion core/src/main/java/com/alibaba/fastjson2/JSONPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ public static Object eval(Object rootObject, String path) {
}

public static void set(Object rootObject, String path, Object value) {
JSONPath.of(path).set(rootObject, value);
JSONPath.of(path)
.set(rootObject, value);
}

public static void remove(Object rootObject, String path) {
JSONPath.of(path)
.remove(rootObject);
}

public static Map<String, Object> paths(Object javaObject) {
Expand Down Expand Up @@ -1184,6 +1190,25 @@ public boolean apply(Object fieldValue) {
throw new UnsupportedOperationException();
}
}

public void set(Context context, Object value) {
Object object = context.parent == null
? context.root
: context.parent.value;

if (object instanceof List) {
List list = (List) object;
for (int i = 0; i < list.size(); i++) {
Object item = list.get(i);
if (apply(context, item)) {
list.set(i, value);
}
}
return;
}

throw new JSONException("UnsupportedOperation ");
}
}

static final class NameDecimalOpSegment extends NameFilter {
Expand Down Expand Up @@ -4067,6 +4092,32 @@ public void set(Context ctx, Object value) {
throw new JSONException("UnsupportedOperation");
}

@Override
public boolean remove(Context ctx) {
Object object = ctx.parent == null
? ctx.root
: ctx.parent.value;

if (object instanceof java.util.List) {
List list = (List) object;
if (index >= 0) {
if (index < list.size()) {
list.remove(index);
return true;
}
} else {
int itemIndex = list.size() + this.index;
if (itemIndex >= 0) {
list.remove(itemIndex);
return true;
}
}
return false;
}

throw new JSONException("UnsupportedOperation");
}

@Override
public void setInt(Context ctx, int value) {
Object object = ctx.parent == null
Expand Down Expand Up @@ -4473,6 +4524,58 @@ public void accept(JSONReader jsonReader, Context ctx) {
ctx.value = array;
ctx.eval = true;
}

public void set(Context context, Object value) {
Object object = context.parent == null
? context.root
: context.parent.value;

if (object instanceof java.util.List) {
List list = (List) object;
for (int i = 0, size = list.size(); i < size; i++) {
boolean match;
if (begin >= 0) {
match = i >= begin && i < end;
} else {
int ni = i - size;
match = ni >= begin && ni < end;
}
if (match) {
list.set(i, value);
}
}
return;
}

throw new JSONException("UnsupportedOperation " + getClass());
}

public boolean remove(Context context) {
Object object = context.parent == null
? context.root
: context.parent.value;

if (object instanceof java.util.List) {
List list = (List) object;
int removeCount = 0;
for (int size = list.size(), i = size - 1; i >= 0; i--) {
boolean match;
if (begin >= 0) {
match = i >= begin && i < end;
} else {
int ni = i - size;
match = ni >= begin && ni < end;
}
if (match) {
list.remove(i);
removeCount++;
}
}
return removeCount > 0;
}

throw new JSONException("UnsupportedOperation " + getClass());
}
}

static final class MultiIndexSegment extends Segment {
Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,7 @@ public List readArray() {
case '{':
val = readObject();
break;
case '\'':
case '"':
val = readString();
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.alibaba.fastjson2.jsonpath;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONPath;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class JSONPathRemoveTest {
@Test
public void test() {
JSONArray array = JSON.parseArray("[0,1,2,3]");
JSONPath.remove(array, "$[?(@ == 1)]");
assertEquals("[0,2,3]", array.toJSONString());
}

@Test
public void test1() {
JSONObject object = JSON.parseObject("{'store':{'book':['x0','x1']}}");
JSONPath.remove(object, "$.store.book[0]");
assertEquals("{\"store\":{\"book\":[\"x1\"]}}", object.toJSONString());
}

@Test
public void test2() {
JSONObject object = JSON.parseObject("{'store':{'book':['x0','x1','x2','x3']}}");
JSONPath.remove(object, "$.store.book[0:2]");
assertEquals("{\"store\":{\"book\":[\"x2\",\"x3\"]}}", object.toJSONString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.alibaba.fastjson2.jsonpath;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONPath;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class JSONPathSetTest1 {
@Test
public void test() {
JSONArray array = JSON.parseArray("[0,1,2,3]");
JSONPath.set(array, "$[?(@ == 1)]", 9);
assertEquals("[0,9,2,3]", array.toJSONString());
}

@Test
public void test1() {
JSONObject object = JSON.parseObject("{'store':{'book':['x0','x1']}}");
JSONPath.set(object, "$.store.book[0]", "a");
assertEquals("{\"store\":{\"book\":[\"a\",\"x1\"]}}", object.toJSONString());
}

@Test
public void test2() {
JSONObject object = JSON.parseObject("{'store':{'book':['x0','x1','x2','x3']}}");
JSONPath.set(object, "$.store.book[0:2]", "a");
assertEquals("{\"store\":{\"book\":[\"a\",\"a\",\"x2\",\"x3\"]}}", object.toJSONString());
}
}

0 comments on commit f5057e6

Please sign in to comment.