Browse Source

Changed some method names and added java doc.

pull/56/head
Kalle Stenflo 10 years ago
parent
commit
3c2d3ecc70
  1. 17
      json-path/src/main/java/com/jayway/jsonpath/Configuration.java
  2. 10
      json-path/src/main/java/com/jayway/jsonpath/Criteria.java
  3. 15
      json-path/src/main/java/com/jayway/jsonpath/Predicate.java
  4. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java
  5. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/compiler/EvaluationContextImpl.java
  6. 10
      json-path/src/main/java/com/jayway/jsonpath/internal/compiler/PredicateContextImpl.java
  7. 5
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/mapper/DefaultMappingProvider.java
  8. 13
      json-path/src/main/java/com/jayway/jsonpath/spi/mapper/MappingProvider.java
  9. 8
      json-path/src/test/java/com/jayway/jsonpath/BaseTest.java
  10. 2
      json-path/src/test/java/com/jayway/jsonpath/FilterTest.java
  11. 2
      json-path/src/test/java/com/jayway/jsonpath/JacksonTest.java
  12. 29
      json-path/src/test/java/com/jayway/jsonpath/PredicateTest.java
  13. 4
      json-path/src/test/java/com/jayway/jsonpath/old/FilterTest.java

17
json-path/src/main/java/com/jayway/jsonpath/Configuration.java

@ -69,29 +69,29 @@ public class Configuration {
}
public Configuration jsonProvider(JsonProvider newJsonProvider) {
return Configuration.builder().jsonProvider(newJsonProvider).conversionProvider(mappingProvider).options(options).build();
return Configuration.builder().jsonProvider(newJsonProvider).mappingProvider(mappingProvider).options(options).build();
}
public JsonProvider jsonProvider() {
return jsonProvider;
}
public MappingProvider conversionProvider() {
public MappingProvider mappingProvider() {
return mappingProvider;
}
public Configuration conversionProvider(MappingProvider newMappingProvider) {
return Configuration.builder().jsonProvider(jsonProvider).conversionProvider(newMappingProvider).options(options).build();
public Configuration mappingProvider(MappingProvider newMappingProvider) {
return Configuration.builder().jsonProvider(jsonProvider).mappingProvider(newMappingProvider).options(options).build();
}
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).conversionProvider(mappingProvider).options(opts).build();
return Configuration.builder().jsonProvider(jsonProvider).mappingProvider(mappingProvider).options(opts).build();
}
public Configuration options(Option... options) {
return Configuration.builder().jsonProvider(jsonProvider).conversionProvider(mappingProvider).options(options).build();
return Configuration.builder().jsonProvider(jsonProvider).mappingProvider(mappingProvider).options(options).build();
}
public Set<Option> getOptions() {
@ -122,7 +122,7 @@ public class Configuration {
return this;
}
public ConfigurationBuilder conversionProvider(MappingProvider provider) {
public ConfigurationBuilder mappingProvider(MappingProvider provider) {
this.mappingProvider = provider;
return this;
}
@ -164,8 +164,9 @@ public class Configuration {
Set<Option> options();
/**
* Returns the default {@link com.jayway.jsonpath.spi.mapper.MappingProvider}
*
* @return
* @return default mapping provider
*/
MappingProvider mappingProvider();

10
json-path/src/main/java/com/jayway/jsonpath/Criteria.java

@ -195,7 +195,7 @@ public class Criteria implements Predicate {
@Override
boolean eval(Object expected, final Object actual, final PredicateContext ctx) {
Predicate exp = (Predicate) expected;
return exp.apply(new PredicateContextImpl(actual, ctx.rootDocument(), ctx.configuration()));
return exp.apply(new PredicateContextImpl(actual, ctx.root(), ctx.configuration()));
}
},
NOT_EMPTY {
@ -273,20 +273,20 @@ public class Criteria implements Predicate {
boolean exists = ((Boolean) expected);
try {
Configuration c = Configuration.builder().jsonProvider(ctx.configuration().jsonProvider()).options().build();
path.evaluate(ctx.contextDocument(), ctx.rootDocument(), c).getValue();
path.evaluate(ctx.item(), ctx.root(), c).getValue();
return exists;
} catch (PathNotFoundException e) {
return !exists;
}
} else {
try {
final Object actual = path.evaluate(ctx.contextDocument(), ctx.rootDocument(), ctx.configuration()).getValue();
final Object actual = path.evaluate(ctx.item(), ctx.root(), ctx.configuration()).getValue();
Object expectedVal = expected;
if(expected instanceof Path){
Path expectedPath = (Path) expected;
Object doc = expectedPath.isRootPath()?ctx.rootDocument():ctx.contextDocument();
expectedVal = expectedPath.evaluate(doc, ctx.rootDocument(), ctx.configuration()).getValue();
Object doc = expectedPath.isRootPath()?ctx.root():ctx.item();
expectedVal = expectedPath.evaluate(doc, ctx.root(), ctx.configuration()).getValue();
}
return criteriaType.eval(expectedVal, actual, ctx);

15
json-path/src/main/java/com/jayway/jsonpath/Predicate.java

@ -1,5 +1,7 @@
package com.jayway.jsonpath;
import com.jayway.jsonpath.spi.mapper.MappingException;
/**
*
*/
@ -10,16 +12,23 @@ public interface Predicate {
public interface PredicateContext {
/**
* Returns the current element being evaluated by this predicate
* Returns the current item being evaluated by this predicate
* @return current document
*/
Object item();
/**
* Returns the current item being evaluated by this predicate. It will be mapped
* to the provided class
* @return current document
*/
Object contextDocument();
<T> T item(Class<T> clazz) throws MappingException;
/**
* Returns the root document (the complete JSON)
* @return root document
*/
Object rootDocument();
Object root();
/**
* Configuration to use when evaluating

2
json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java

@ -114,7 +114,7 @@ public class JsonReader implements ParseContext, ReadContext {
}
private <T> T convert(Object obj, Class<T> targetType, Configuration configuration){
return configuration.conversionProvider().map(obj, targetType, configuration);
return configuration.mappingProvider().map(obj, targetType, configuration);
}
}

2
json-path/src/main/java/com/jayway/jsonpath/internal/compiler/EvaluationContextImpl.java

@ -27,7 +27,7 @@ public class EvaluationContextImpl implements EvaluationContext {
public EvaluationContextImpl(Path path, Object rootDocument, Configuration configuration) {
notNull(path, "path can not be null");
notNull(rootDocument, "rootDocument can not be null");
notNull(rootDocument, "root can not be null");
notNull(configuration, "configuration can not be null");
this.path = path;
this.rootDocument = rootDocument;

10
json-path/src/main/java/com/jayway/jsonpath/internal/compiler/PredicateContextImpl.java

@ -2,6 +2,7 @@ package com.jayway.jsonpath.internal.compiler;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.Predicate;
import com.jayway.jsonpath.spi.mapper.MappingException;
public class PredicateContextImpl implements Predicate.PredicateContext {
private final Object contextDocument;
@ -15,12 +16,17 @@ public class PredicateContextImpl implements Predicate.PredicateContext {
}
@Override
public Object contextDocument() {
public Object item() {
return contextDocument;
}
@Override
public Object rootDocument() {
public <T> T item(Class<T> clazz) throws MappingException {
return configuration().mappingProvider().map(contextDocument, clazz, configuration);
}
@Override
public Object root() {
return rootDocument;
}

5
json-path/src/main/java/com/jayway/jsonpath/internal/spi/mapper/DefaultMappingProvider.java

@ -19,10 +19,7 @@ public class DefaultMappingProvider implements MappingProvider {
addMapper(new DateMapper());
}
public void addMapper(Mapper converter) {
protected void addMapper(Mapper converter) {
for (Mapper.ConvertiblePair convertible : converter.getConvertibleTypes()) {
if(!converters.containsKey(convertible.getTargetType())){
converters.put(convertible.getTargetType(), new HashMap<Class<?>, Mapper>());

13
json-path/src/main/java/com/jayway/jsonpath/spi/mapper/MappingProvider.java

@ -2,6 +2,19 @@ package com.jayway.jsonpath.spi.mapper;
import com.jayway.jsonpath.Configuration;
/**
* Maps object between different Types
*/
public interface MappingProvider {
/**
*
* @param source object to map
* @param targetType the type the source object should be mapped to
* @param configuration current configuration
* @param <T> the mapped result type
* @return return the mapped object
*/
<T> T map(Object source, Class<T> targetType, Configuration configuration);
}

8
json-path/src/test/java/com/jayway/jsonpath/BaseTest.java

@ -1,6 +1,8 @@
package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.compiler.PredicateContextImpl;
import com.jayway.jsonpath.internal.spi.json.GsonJsonProvider;
import com.jayway.jsonpath.internal.spi.mapper.GsonMappingProvider;
public class BaseTest {
/*
@ -24,7 +26,11 @@ public class BaseTest {
}*/
public static final Configuration GSON_CONFIGURATION = Configuration
.builder()
.mappingProvider(new GsonMappingProvider())
.jsonProvider(new GsonJsonProvider())
.build();
public static final String JSON_DOCUMENT = "{\n" +
" \"string-property\" : \"string-value\", \n" +

2
json-path/src/test/java/com/jayway/jsonpath/FilterTest.java

@ -374,7 +374,7 @@ public class FilterTest extends BaseTest {
Predicate p = new Predicate() {
@Override
public boolean apply(PredicateContext ctx) {
Map<String, Object> t = (Map<String, Object>) ctx.contextDocument();
Map<String, Object> t = (Map<String, Object>) ctx.item();
Object o = t.get("int-key");

2
json-path/src/test/java/com/jayway/jsonpath/JacksonTest.java

@ -10,7 +10,7 @@ public class JacksonTest {
private static Configuration config = Configuration
.builder()
.conversionProvider(new JacksonMappingProvider())
.mappingProvider(new JacksonMappingProvider())
.jsonProvider(new JacksonJsonProvider())
.build();

29
json-path/src/test/java/com/jayway/jsonpath/PredicateTest.java

@ -0,0 +1,29 @@
package com.jayway.jsonpath;
import org.junit.Test;
import java.util.List;
import java.util.Map;
import static com.jayway.jsonpath.JsonPath.using;
import static org.assertj.core.api.Assertions.assertThat;
public class PredicateTest extends BaseTest {
private static ReadContext reader = using(GSON_CONFIGURATION).parse(JSON_DOCUMENT);
@Test
public void predicates_filters_can_be_applied() {
Predicate booksWithISBN = new Predicate() {
@Override
public boolean apply(PredicateContext ctx) {
return ctx.item(Map.class).containsKey("isbn");
}
};
assertThat(reader.read("$.store.book[?].isbn", List.class, booksWithISBN)).containsOnly("0-395-19395-8", "0-553-21311-3");
}
}

4
json-path/src/test/java/com/jayway/jsonpath/old/FilterTest.java

@ -346,7 +346,7 @@ public class FilterTest extends BaseTest {
Predicate customFilter = new Predicate () {
@Override
public boolean apply(PredicateContext ctx) {
if (ctx.configuration().jsonProvider().getMapValue(ctx.contextDocument(), "name").equals("rootGrandChild_A")) {
if (ctx.configuration().jsonProvider().getMapValue(ctx.item(), "name").equals("rootGrandChild_A")) {
return true;
}
return false;
@ -369,7 +369,7 @@ public class FilterTest extends BaseTest {
Predicate customFilter = new Predicate() {
@Override
public boolean apply(PredicateContext ctx) {
return 1 == (Integer)ctx.contextDocument();
return 1 == (Integer)ctx.item();
}
};

Loading…
Cancel
Save