Skip to content

Enables adding/removing arrays to/from arrays #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/main/java/com/github/fge/jsonpatch/AddOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,23 @@ private JsonNode addToObject(final JsonPointer path, final JsonNode node)
final TokenResolver<JsonNode> token = Iterables.getLast(path);
final JsonNode ret = node.deepCopy();
final ObjectNode target = (ObjectNode) path.parent().get(ret);
target.set(token.getToken().getRaw(), value);

final String objectName = token.getToken().getRaw();
final JsonNode targetObject = target.get(objectName);

if (targetObject != null && targetObject.isArray()) {
addToArrayObject((ArrayNode) targetObject);
} else {
target.set(objectName, value);
}
return ret;
}

private void addToArrayObject(ArrayNode targetObject) {
if (value.isArray()) {
targetObject.addAll((ArrayNode) value);
} else {
targetObject.add(value);
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/github/fge/jsonpatch/MoveOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.NullNode;
import com.github.fge.jackson.jsonpointer.JsonPointer;

/**
Expand Down Expand Up @@ -82,7 +83,7 @@ public JsonNode apply(final JsonNode node)
if (movedNode.isMissingNode())
throw new JsonPatchException(BUNDLE.getMessage(
"jsonPatch.noSuchPath"));
final JsonPatchOperation remove = new RemoveOperation(from);
final JsonPatchOperation remove = new RemoveOperation(from, NullNode.getInstance());
final JsonPatchOperation add = new AddOperation(path, movedNode);
return add.apply(remove.apply(node));
}
Expand Down
54 changes: 49 additions & 5 deletions src/main/java/com/github/fge/jsonpatch/RemoveOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.MissingNode;
import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.fge.jackson.jsonpointer.JsonPointer;

Expand All @@ -42,10 +43,13 @@
public final class RemoveOperation
extends JsonPatchOperation
{
private final JsonNode value;

@JsonCreator
public RemoveOperation(@JsonProperty("path") final JsonPointer path)
public RemoveOperation(@JsonProperty("path") final JsonPointer path, @JsonProperty(value = "value") final JsonNode value)
{
super("remove", path);
this.value = value;
}

@Override
Expand All @@ -56,17 +60,53 @@ public JsonNode apply(final JsonNode node)
return MissingNode.getInstance();
if (path.path(node).isMissingNode())
throw new JsonPatchException(BUNDLE.getMessage(
"jsonPatch.noSuchPath"));
"jsonPatch.noSuchPath"));
final JsonNode ret = node.deepCopy();
final JsonNode parentNode = path.parent().get(ret);
final String raw = Iterables.getLast(path).getToken().getRaw();
final String fieldName = Iterables.getLast(path).getToken().getRaw();

if (parentNode.isObject())
((ObjectNode) parentNode).remove(raw);
removeFromObject(parentNode, fieldName);
else
((ArrayNode) parentNode).remove(Integer.parseInt(raw));
((ArrayNode) parentNode).remove(Integer.parseInt(fieldName));
return ret;
}

private void removeFromObject(JsonNode parentNode, String fieldName) {
final JsonNode targetObject = parentNode.get(fieldName);
if (targetObject.isArray()) {
if (value.isArray()) {
final ArrayNode arr = remove((ArrayNode) targetObject, (ArrayNode) value);
((ObjectNode) parentNode).replace(fieldName, arr);
} else {
final ArrayNode arr = remove((ArrayNode) targetObject, value);
((ObjectNode) parentNode).replace(fieldName, arr);
}
} else {
((ObjectNode) parentNode).remove(fieldName);
}
}

private ArrayNode remove(ArrayNode arrayObject, JsonNode toRemove) {
ArrayNode reduced = arrayObject.deepCopy();
int i = 0;
for (JsonNode e : arrayObject) {
if (e.equals(toRemove)) {
reduced.remove(i);
}
i++;
}
return reduced;
}

private ArrayNode remove(ArrayNode arrayObject, ArrayNode toRemove) {
ArrayNode reduced = arrayObject.deepCopy();
for (JsonNode rem : toRemove) {
reduced = remove(reduced, rem);
}
return reduced;
}

@Override
public void serialize(final JsonGenerator jgen,
final SerializerProvider provider)
Expand All @@ -75,6 +115,10 @@ public void serialize(final JsonGenerator jgen,
jgen.writeStartObject();
jgen.writeStringField("op", "remove");
jgen.writeStringField("path", path.toString());
if (value != null && !value.equals(NullNode.getInstance())) {
jgen.writeFieldName("value");
jgen.writeTree(value);
}
jgen.writeEndObject();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.github.fge.jsonpatch.diff;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.NullNode;
import com.github.fge.jackson.jsonpointer.JsonPointer;
import com.github.fge.jsonpatch.AddOperation;
import com.github.fge.jsonpatch.CopyOperation;
Expand Down Expand Up @@ -145,7 +146,7 @@ JsonPatchOperation toOperation(final DiffOperation op)
@Override
JsonPatchOperation toOperation(final DiffOperation op)
{
return new RemoveOperation(op.from);
return new RemoveOperation(op.from, NullNode.getInstance());
}
},
REPLACE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.github.fge.jsonpatch;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.NullNode;
import com.github.fge.jackson.JacksonUtils;
import com.github.fge.jackson.jsonpointer.JsonPointer;
import org.testng.annotations.Test;
Expand All @@ -42,7 +43,7 @@ public void removingRootReturnsMissingNode()
throws JsonPatchException
{
final JsonNode node = JacksonUtils.nodeFactory().nullNode();
final JsonPatchOperation op = new RemoveOperation(JsonPointer.empty());
final JsonPatchOperation op = new RemoveOperation(JsonPointer.empty(), NullNode.getInstance());
final JsonNode ret = op.apply(node);
assertTrue(ret.isMissingNode());
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/resources/jsonpatch/add.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@
"node": { "array": [ 2, null, {}, 1] },
"expected": { "array": [ 2, null, "hello", {}, 1 ] }
},
{
"op": { "op": "add", "path": "/array", "value": ["world"] },
"node": { "array": ["hello"] },
"expected": { "array": [ "hello", "world" ] }
},
{
"op": { "op": "add", "path": "/array", "value": ["hello"] },
"node": {},
"expected": { "array": [ "hello" ] }
},
{
"op": { "op": "add", "path": "/obj/inner/b", "value": [ 1, 2 ] },
"node": {
Expand Down
15 changes: 15 additions & 0 deletions src/test/resources/jsonpatch/remove.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@
}
],
"ops": [
{
"op": { "op": "remove", "path": "/x", "value": "y" },
"node": { "x": [ "y", "z" ], "foo": "bar" },
"expected": { "x": [ "z" ], "foo": "bar" }
},
{
"op": { "op": "remove", "path": "/x", "value": ["z"] },
"node": { "x": [ "y", "z" ], "foo": "bar" },
"expected": { "x": [ "y" ], "foo": "bar" }
},
{
"op": { "op": "remove", "path": "/x", "value": {"a": 3} },
"node": { "x": [ {"a": 3}, "z" ], "foo": "bar" },
"expected": { "x": [ "z" ], "foo": "bar" }
},
{
"op": { "op": "remove", "path": "/x/y" },
"node": { "x": { "a": "b", "y": {} } },
Expand Down