|
|
|
@ -1,10 +1,10 @@
|
|
|
|
|
package com.jayway.jsonpath.internal; |
|
|
|
|
|
|
|
|
|
import com.jayway.jsonpath.Configuration; |
|
|
|
|
import com.jayway.jsonpath.InvalidModificationException; |
|
|
|
|
import com.jayway.jsonpath.*; |
|
|
|
|
import com.jayway.jsonpath.spi.json.JsonProvider; |
|
|
|
|
|
|
|
|
|
import java.util.Collection; |
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
public abstract class PathRef implements Comparable<PathRef> { |
|
|
|
|
|
|
|
|
@ -17,6 +17,9 @@ public abstract class PathRef implements Comparable<PathRef> {
|
|
|
|
|
@Override |
|
|
|
|
public void set(Object newVal, Configuration configuration) {} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void convert(ValueConverter valueConverter, Configuration configuration) {} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void delete(Configuration configuration) {} |
|
|
|
|
|
|
|
|
@ -25,6 +28,10 @@ public abstract class PathRef implements Comparable<PathRef> {
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void put(String key, Object newVal, Configuration configuration) {} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void renameKey(String oldKeyName, String newKeyName, Configuration configuration) {} |
|
|
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
protected Object parent; |
|
|
|
@ -38,12 +45,32 @@ public abstract class PathRef implements Comparable<PathRef> {
|
|
|
|
|
|
|
|
|
|
public abstract void set(Object newVal, Configuration configuration); |
|
|
|
|
|
|
|
|
|
public abstract void convert(ValueConverter valueConverter, Configuration configuration); |
|
|
|
|
|
|
|
|
|
public abstract void delete(Configuration configuration); |
|
|
|
|
|
|
|
|
|
public abstract void add(Object newVal, Configuration configuration); |
|
|
|
|
|
|
|
|
|
public abstract void put(String key, Object newVal, Configuration configuration); |
|
|
|
|
|
|
|
|
|
public abstract void renameKey(String oldKey,String newKeyName, Configuration configuration); |
|
|
|
|
|
|
|
|
|
protected void renameInMap(Object targetMap, String oldKeyName, String newKeyName, Configuration configuration){ |
|
|
|
|
if(configuration.jsonProvider().isMap(targetMap)){ |
|
|
|
|
if(configuration.jsonProvider().getMapValue(targetMap, oldKeyName) == JsonProvider.UNDEFINED){ |
|
|
|
|
throw new PathNotFoundException("No results for Key "+oldKeyName+" found in map!"); |
|
|
|
|
} |
|
|
|
|
configuration.jsonProvider().setProperty(targetMap, newKeyName, configuration.jsonProvider().getMapValue(targetMap, oldKeyName)); |
|
|
|
|
configuration.jsonProvider().removeProperty(targetMap, oldKeyName); |
|
|
|
|
} else { |
|
|
|
|
throw new InvalidModificationException("Can only rename properties in a map"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected boolean targetInvalid(Object target){ |
|
|
|
|
return target == JsonProvider.UNDEFINED || target == null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public int compareTo(PathRef o) { |
|
|
|
|
return this.getAccessor().toString().compareTo(o.getAccessor().toString()) * -1; |
|
|
|
@ -81,6 +108,10 @@ public abstract class PathRef implements Comparable<PathRef> {
|
|
|
|
|
throw new InvalidModificationException("Invalid delete operation"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void convert(ValueConverter valueConverter, Configuration configuration){ |
|
|
|
|
throw new InvalidModificationException("Invalid convert operation"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void delete(Configuration configuration) { |
|
|
|
|
throw new InvalidModificationException("Invalid delete operation"); |
|
|
|
@ -103,27 +134,45 @@ public abstract class PathRef implements Comparable<PathRef> {
|
|
|
|
|
throw new InvalidModificationException("Invalid put operation. $ is not a map"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void renameKey(String oldKeyName, String newKeyName, Configuration configuration) { |
|
|
|
|
Object target = parent; |
|
|
|
|
if(targetInvalid(target)){ |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
renameInMap(target, oldKeyName, newKeyName, configuration); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static class ArrayIndexPathRef extends PathRef { |
|
|
|
|
|
|
|
|
|
private int index; |
|
|
|
|
private Object value; |
|
|
|
|
|
|
|
|
|
private ArrayIndexPathRef(Object parent, int index) { |
|
|
|
|
super(parent); |
|
|
|
|
this.index = index; |
|
|
|
|
this.value = ((List<Object>) parent).get(index); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void set(Object newVal, Configuration configuration){ |
|
|
|
|
configuration.jsonProvider().setArrayIndex(parent, index, newVal); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void convert(ValueConverter valueConverter, Configuration configuration){ |
|
|
|
|
Object currentValue = configuration.jsonProvider().getArrayIndex(parent, index); |
|
|
|
|
configuration.jsonProvider().setArrayIndex(parent, index, valueConverter.convert(currentValue)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void delete(Configuration configuration){ |
|
|
|
|
configuration.jsonProvider().removeProperty(parent, index); |
|
|
|
|
configuration.jsonProvider().removeProperty(parent, value); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void add(Object value, Configuration configuration){ |
|
|
|
|
Object target = configuration.jsonProvider().getArrayIndex(parent, index); |
|
|
|
|
if(target == JsonProvider.UNDEFINED || target == null){ |
|
|
|
|
if(targetInvalid(target)){ |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if(configuration.jsonProvider().isArray(target)){ |
|
|
|
@ -135,7 +184,7 @@ public abstract class PathRef implements Comparable<PathRef> {
|
|
|
|
|
|
|
|
|
|
public void put(String key, Object value, Configuration configuration){ |
|
|
|
|
Object target = configuration.jsonProvider().getArrayIndex(parent, index); |
|
|
|
|
if(target == JsonProvider.UNDEFINED || target == null){ |
|
|
|
|
if(targetInvalid(target)){ |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if(configuration.jsonProvider().isMap(target)){ |
|
|
|
@ -145,6 +194,15 @@ public abstract class PathRef implements Comparable<PathRef> {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void renameKey(String oldKeyName, String newKeyName, Configuration configuration) { |
|
|
|
|
Object target = configuration.jsonProvider().getArrayIndex(parent, index); |
|
|
|
|
if(targetInvalid(target)){ |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
renameInMap(target, oldKeyName, newKeyName, configuration); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Object getAccessor() { |
|
|
|
|
return index; |
|
|
|
@ -175,13 +233,20 @@ public abstract class PathRef implements Comparable<PathRef> {
|
|
|
|
|
configuration.jsonProvider().setProperty(parent, property, newVal); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void convert(ValueConverter valueConverter, Configuration configuration) { |
|
|
|
|
Object currentValue = configuration.jsonProvider().getMapValue(parent, property); |
|
|
|
|
configuration.jsonProvider().setProperty(parent, property, valueConverter.convert(currentValue)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void delete(Configuration configuration){ |
|
|
|
|
configuration.jsonProvider().removeProperty(parent, property); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void add(Object value, Configuration configuration){ |
|
|
|
|
Object target = configuration.jsonProvider().getMapValue(parent, property); |
|
|
|
|
if(target == JsonProvider.UNDEFINED || target == null){ |
|
|
|
|
if(targetInvalid(target)){ |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if(configuration.jsonProvider().isArray(target)){ |
|
|
|
@ -193,7 +258,7 @@ public abstract class PathRef implements Comparable<PathRef> {
|
|
|
|
|
|
|
|
|
|
public void put(String key, Object value, Configuration configuration){ |
|
|
|
|
Object target = configuration.jsonProvider().getMapValue(parent, property); |
|
|
|
|
if(target == JsonProvider.UNDEFINED || target == null){ |
|
|
|
|
if(targetInvalid(target)){ |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if(configuration.jsonProvider().isMap(target)){ |
|
|
|
@ -203,6 +268,15 @@ public abstract class PathRef implements Comparable<PathRef> {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void renameKey(String oldKeyName, String newKeyName, Configuration configuration) { |
|
|
|
|
Object target = configuration.jsonProvider().getMapValue(parent, property); |
|
|
|
|
if(targetInvalid(target)){ |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
renameInMap(target, oldKeyName, newKeyName, configuration); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Object getAccessor() { |
|
|
|
|
return property; |
|
|
|
@ -223,6 +297,12 @@ public abstract class PathRef implements Comparable<PathRef> {
|
|
|
|
|
configuration.jsonProvider().setProperty(parent, property, newVal); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
public void convert(ValueConverter valueConverter, Configuration configuration) { |
|
|
|
|
for (String property : properties) { |
|
|
|
|
Object currentValue = configuration.jsonProvider().getMapValue(parent, property); |
|
|
|
|
configuration.jsonProvider().setProperty(parent, property, valueConverter.convert(currentValue)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void delete(Configuration configuration){ |
|
|
|
|
for (String property : properties) { |
|
|
|
@ -237,7 +317,12 @@ public abstract class PathRef implements Comparable<PathRef> {
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void put(String key, Object newVal, Configuration configuration) { |
|
|
|
|
throw new InvalidModificationException("Add can not be performed to multiple properties"); |
|
|
|
|
throw new InvalidModificationException("Put can not be performed to multiple properties"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void renameKey(String oldKeyName, String newKeyName, Configuration configuration) { |
|
|
|
|
throw new InvalidModificationException("Rename can not be performed to multiple properties"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|