Skip to content
Open
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
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -514,4 +514,27 @@ CacheProvider.setCache(new Cache() {
});
```

### Path Function Provider SPI

This SPI allows adding, overriding, and removing the path functions available in the JsonPath path.
Therefore, change the `PathFunctionProvider` of the `Configuration`.

```java
String json = "...";

Configuration conf = Configuration.defaultConfiguration().pathFunctionProvider(new DefaultPathFunctionProvider() {
@Override
public PathFunction newFunction(String name) throws InvalidPathException {
if (name.equals("toUpperCase")) {
return (currentPath, parent, model, ctx, parameters) -> ((String) model).toUpperCase();
}

// Fall back to default functions
return super.newFunction(name);
}
});

JsonPath.using(conf).parse(json).read("$.store.book[0].author.toUpperCase()").toString(); // NIGEL REES
```

[![Analytics](https://ga-beacon.appspot.com/UA-54945131-1/jsonpath/index)](https://github.com/igrigorik/ga-beacon)
77 changes: 69 additions & 8 deletions json-path/src/main/java/com/jayway/jsonpath/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
package com.jayway.jsonpath;

import com.jayway.jsonpath.internal.DefaultsImpl;
import com.jayway.jsonpath.spi.function.DefaultPathFunctionProvider;
import com.jayway.jsonpath.spi.json.JsonProvider;
import com.jayway.jsonpath.spi.mapper.MappingProvider;
import com.jayway.jsonpath.spi.function.PathFunctionProvider;

import java.util.*;

Expand Down Expand Up @@ -50,16 +52,19 @@ private static Defaults getEffectiveDefaults(){
private final MappingProvider mappingProvider;
private final Set<Option> options;
private final Collection<EvaluationListener> evaluationListeners;
private final PathFunctionProvider pathFunctionProvider;

private Configuration(JsonProvider jsonProvider, MappingProvider mappingProvider, EnumSet<Option> options, Collection<EvaluationListener> evaluationListeners) {
private Configuration(JsonProvider jsonProvider, MappingProvider mappingProvider, EnumSet<Option> options, Collection<EvaluationListener> evaluationListeners, PathFunctionProvider pathFunctionProvider) {
notNull(jsonProvider, "jsonProvider can not be null");
notNull(mappingProvider, "mappingProvider can not be null");
notNull(options, "setOptions can not be null");
notNull(evaluationListeners, "evaluationListeners can not be null");
notNull(pathFunctionProvider, "pathFunctionProvider can not be null");
this.jsonProvider = jsonProvider;
this.mappingProvider = mappingProvider;
this.options = Collections.unmodifiableSet(options);
this.evaluationListeners = Collections.unmodifiableCollection(evaluationListeners);
this.pathFunctionProvider = pathFunctionProvider;
}

/**
Expand All @@ -68,7 +73,13 @@ private Configuration(JsonProvider jsonProvider, MappingProvider mappingProvider
* @return a new configuration
*/
public Configuration addEvaluationListeners(EvaluationListener... evaluationListener){
return Configuration.builder().jsonProvider(jsonProvider).mappingProvider(mappingProvider).options(options).evaluationListener(evaluationListener).build();
return Configuration.builder()
.jsonProvider(jsonProvider)
.mappingProvider(mappingProvider)
.options(options)
.evaluationListener(evaluationListener)
.pathFunctionProvider(pathFunctionProvider)
.build();
}

/**
Expand All @@ -77,7 +88,13 @@ public Configuration addEvaluationListeners(EvaluationListener... evaluationList
* @return a new configuration
*/
public Configuration setEvaluationListeners(EvaluationListener... evaluationListener){
return Configuration.builder().jsonProvider(jsonProvider).mappingProvider(mappingProvider).options(options).evaluationListener(evaluationListener).build();
return Configuration.builder()
.jsonProvider(jsonProvider)
.mappingProvider(mappingProvider)
.options(options)
.evaluationListener(evaluationListener)
.pathFunctionProvider(pathFunctionProvider)
.build();
}

/**
Expand All @@ -94,7 +111,13 @@ public Collection<EvaluationListener> getEvaluationListeners(){
* @return a new configuration
*/
public Configuration jsonProvider(JsonProvider newJsonProvider) {
return Configuration.builder().jsonProvider(newJsonProvider).mappingProvider(mappingProvider).options(options).evaluationListener(evaluationListeners).build();
return Configuration.builder()
.jsonProvider(newJsonProvider)
.mappingProvider(mappingProvider)
.options(options)
.evaluationListener(evaluationListeners)
.pathFunctionProvider(pathFunctionProvider)
.build();
}

/**
Expand All @@ -111,7 +134,13 @@ public JsonProvider jsonProvider() {
* @return a new configuration
*/
public Configuration mappingProvider(MappingProvider newMappingProvider) {
return Configuration.builder().jsonProvider(jsonProvider).mappingProvider(newMappingProvider).options(options).evaluationListener(evaluationListeners).build();
return Configuration.builder()
.jsonProvider(jsonProvider)
.mappingProvider(newMappingProvider)
.options(options)
.evaluationListener(evaluationListeners)
.pathFunctionProvider(pathFunctionProvider)
.build();
}

/**
Expand All @@ -122,6 +151,20 @@ public MappingProvider mappingProvider() {
return mappingProvider;
}

public Configuration pathFunctionProvider(PathFunctionProvider newPathFunctionProvider) {
return Configuration.builder()
.jsonProvider(jsonProvider)
.mappingProvider(mappingProvider)
.options(options)
.evaluationListener(evaluationListeners)
.pathFunctionProvider(newPathFunctionProvider)
.build();
}

public PathFunctionProvider pathFunctionProvider() {
return this.pathFunctionProvider;
}

/**
* Creates a new configuration by adding the new options to the options used in this configuration.
* @param options options to add
Expand All @@ -131,7 +174,13 @@ public Configuration addOptions(Option... options) {
EnumSet<Option> opts = EnumSet.noneOf(Option.class);
opts.addAll(this.options);
opts.addAll(asList(options));
return Configuration.builder().jsonProvider(jsonProvider).mappingProvider(mappingProvider).options(opts).evaluationListener(evaluationListeners).build();
return Configuration.builder()
.jsonProvider(jsonProvider)
.mappingProvider(mappingProvider)
.options(opts)
.evaluationListener(evaluationListeners)
.pathFunctionProvider(pathFunctionProvider)
.build();
}

/**
Expand All @@ -140,7 +189,13 @@ public Configuration addOptions(Option... options) {
* @return the new configuration instance
*/
public Configuration setOptions(Option... options) {
return Configuration.builder().jsonProvider(jsonProvider).mappingProvider(mappingProvider).options(options).evaluationListener(evaluationListeners).build();
return Configuration.builder()
.jsonProvider(jsonProvider)
.mappingProvider(mappingProvider)
.options(options)
.evaluationListener(evaluationListeners)
.pathFunctionProvider(pathFunctionProvider)
.build();
}

/**
Expand Down Expand Up @@ -186,6 +241,7 @@ public static class ConfigurationBuilder {
private MappingProvider mappingProvider;
private EnumSet<Option> options = EnumSet.noneOf(Option.class);
private Collection<EvaluationListener> evaluationListener = new ArrayList<EvaluationListener>();
private PathFunctionProvider pathFunctionProvider = new DefaultPathFunctionProvider();

public ConfigurationBuilder jsonProvider(JsonProvider provider) {
this.jsonProvider = provider;
Expand Down Expand Up @@ -219,6 +275,11 @@ public ConfigurationBuilder evaluationListener(Collection<EvaluationListener> li
return this;
}

public ConfigurationBuilder pathFunctionProvider(PathFunctionProvider pathFunctionProvider) {
this.pathFunctionProvider = pathFunctionProvider;
return this;
}

public Configuration build() {
if (jsonProvider == null || mappingProvider == null) {
final Defaults defaults = getEffectiveDefaults();
Expand All @@ -229,7 +290,7 @@ public Configuration build() {
mappingProvider = defaults.mappingProvider();
}
}
return new Configuration(jsonProvider, mappingProvider, options, evaluationListener);
return new Configuration(jsonProvider, mappingProvider, options, evaluationListener, pathFunctionProvider);
}
}

Expand Down
1 change: 0 additions & 1 deletion json-path/src/main/java/com/jayway/jsonpath/Criteria.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
package com.jayway.jsonpath;

import com.jayway.jsonpath.internal.Path;
import com.jayway.jsonpath.internal.Utils;
import com.jayway.jsonpath.internal.filter.RelationalExpressionNode;
import com.jayway.jsonpath.internal.filter.RelationalOperator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jayway.jsonpath.internal;

import com.jayway.jsonpath.Configuration;
package com.jayway.jsonpath;

import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -44,7 +42,7 @@ public interface EvaluationContext {
<T> T getValue();

/**
* See {@link com.jayway.jsonpath.internal.EvaluationContext#getValue()}
* See {@link EvaluationContext#getValue()}
*
* @param unwrap tells th underlying json provider if primitives should be unwrapped
* @param <T> expected return type
Expand Down
1 change: 0 additions & 1 deletion json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,6 @@ public static JsonPath compile(String jsonPath, Predicate... filters) {
return new JsonPath(jsonPath, filters);
}


// --------------------------------------------------------
//
// Static utility functions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jayway.jsonpath.internal;

import com.jayway.jsonpath.Configuration;
package com.jayway.jsonpath;

/**
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.jayway.jsonpath.internal;
package com.jayway.jsonpath;

import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.InvalidModificationException;
import com.jayway.jsonpath.MapFunction;
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.internal.Utils;
import com.jayway.jsonpath.spi.json.JsonProvider;

import java.util.Collection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.JsonPathException;
import com.jayway.jsonpath.Predicate;
import com.jayway.jsonpath.internal.Path;
import com.jayway.jsonpath.Path;
import com.jayway.jsonpath.internal.path.PathCompiler;
import net.minidev.json.parser.JSONParser;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.Predicate;
import com.jayway.jsonpath.internal.Path;
import com.jayway.jsonpath.Path;
import com.jayway.jsonpath.internal.Utils;
import com.jayway.jsonpath.internal.path.PathCompiler;
import com.jayway.jsonpath.internal.path.PredicateContextImpl;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.jayway.jsonpath.internal.function;

import com.jayway.jsonpath.internal.EvaluationContext;
import com.jayway.jsonpath.internal.PathRef;
import com.jayway.jsonpath.EvaluationContext;
import com.jayway.jsonpath.PathRef;
import com.jayway.jsonpath.spi.function.Parameter;
import com.jayway.jsonpath.spi.function.PathFunction;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.jayway.jsonpath.internal.function.json;

import com.jayway.jsonpath.internal.EvaluationContext;
import com.jayway.jsonpath.internal.PathRef;
import com.jayway.jsonpath.internal.function.Parameter;
import com.jayway.jsonpath.internal.function.PathFunction;
import com.jayway.jsonpath.EvaluationContext;
import com.jayway.jsonpath.PathRef;
import com.jayway.jsonpath.spi.function.Parameter;
import com.jayway.jsonpath.spi.function.PathFunction;
import com.jayway.jsonpath.spi.json.JsonProvider;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.jayway.jsonpath.internal.function.json;

import com.jayway.jsonpath.internal.EvaluationContext;
import com.jayway.jsonpath.internal.PathRef;
import com.jayway.jsonpath.internal.function.Parameter;
import com.jayway.jsonpath.internal.function.PathFunction;
import com.jayway.jsonpath.EvaluationContext;
import com.jayway.jsonpath.PathRef;
import com.jayway.jsonpath.spi.function.Parameter;
import com.jayway.jsonpath.spi.function.PathFunction;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
package com.jayway.jsonpath.internal.function.latebinding;

import com.jayway.jsonpath.internal.function.Parameter;
import com.jayway.jsonpath.spi.function.Parameter;
import com.jayway.jsonpath.spi.json.JsonProvider;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package com.jayway.jsonpath.internal.function.latebinding;

import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.internal.Path;
import com.jayway.jsonpath.Path;

import java.util.Objects;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.jayway.jsonpath.internal.function.numeric;

import com.jayway.jsonpath.JsonPathException;
import com.jayway.jsonpath.internal.EvaluationContext;
import com.jayway.jsonpath.internal.PathRef;
import com.jayway.jsonpath.internal.function.Parameter;
import com.jayway.jsonpath.internal.function.PathFunction;
import com.jayway.jsonpath.EvaluationContext;
import com.jayway.jsonpath.PathRef;
import com.jayway.jsonpath.spi.function.Parameter;
import com.jayway.jsonpath.spi.function.PathFunction;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.jayway.jsonpath.internal.function.sequence;

import com.jayway.jsonpath.JsonPathException;
import com.jayway.jsonpath.internal.EvaluationContext;
import com.jayway.jsonpath.internal.PathRef;
import com.jayway.jsonpath.internal.function.Parameter;
import com.jayway.jsonpath.internal.function.PathFunction;
import com.jayway.jsonpath.EvaluationContext;
import com.jayway.jsonpath.PathRef;
import com.jayway.jsonpath.spi.function.Parameter;
import com.jayway.jsonpath.spi.function.PathFunction;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.jayway.jsonpath.internal.function.sequence;

import com.jayway.jsonpath.internal.EvaluationContext;
import com.jayway.jsonpath.internal.function.Parameter;
import com.jayway.jsonpath.EvaluationContext;
import com.jayway.jsonpath.spi.function.Parameter;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.jayway.jsonpath.internal.function.sequence;

import com.jayway.jsonpath.internal.EvaluationContext;
import com.jayway.jsonpath.internal.function.Parameter;
import com.jayway.jsonpath.EvaluationContext;
import com.jayway.jsonpath.spi.function.Parameter;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.jayway.jsonpath.internal.function.sequence;

import com.jayway.jsonpath.internal.EvaluationContext;
import com.jayway.jsonpath.internal.function.Parameter;
import com.jayway.jsonpath.EvaluationContext;
import com.jayway.jsonpath.spi.function.Parameter;

import java.util.List;

Expand Down
Loading