|
|
|
@ -2,6 +2,8 @@ package com.jayway.jsonpath.internal;
|
|
|
|
|
|
|
|
|
|
import com.jayway.jsonpath.Configuration; |
|
|
|
|
import com.jayway.jsonpath.InvalidModificationException; |
|
|
|
|
import com.jayway.jsonpath.PathNotFoundException; |
|
|
|
|
import com.jayway.jsonpath.ValueConverter; |
|
|
|
|
import com.jayway.jsonpath.spi.json.JsonProvider; |
|
|
|
|
|
|
|
|
|
import java.util.Collection; |
|
|
|
@ -17,6 +19,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 +30,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 +47,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 +110,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,7 +136,18 @@ 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; |
|
|
|
@ -117,13 +161,18 @@ public abstract class PathRef implements Comparable<PathRef> {
|
|
|
|
|
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, configuration)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void delete(Configuration configuration){ |
|
|
|
|
configuration.jsonProvider().removeProperty(parent, index); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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, configuration)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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, configuration)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|