-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Defines the pattern for taking item from collection of JSONArray by i…
…ndex (#842)
- Loading branch information
Showing
8 changed files
with
178 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...main/java/com/jayway/jsonpath/internal/function/sequence/AbstractSequenceAggregation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
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 java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Defines the pattern for taking item from collection of JSONArray by index | ||
* | ||
* Created by git9527 on 6/11/22. | ||
*/ | ||
public abstract class AbstractSequenceAggregation implements PathFunction { | ||
|
||
protected abstract int targetIndex(EvaluationContext ctx, List<Parameter> parameters); | ||
|
||
@Override | ||
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) { | ||
if(ctx.configuration().jsonProvider().isArray(model)){ | ||
|
||
Iterable<?> objects = ctx.configuration().jsonProvider().toIterable(model); | ||
List<Object> objectList = new ArrayList<>(); | ||
objects.forEach(objectList::add); | ||
int targetIndex = this.targetIndex(ctx, parameters); | ||
if (targetIndex >= 0) { | ||
return objectList.get(targetIndex); | ||
} else { | ||
int realIndex = objectList.size() + targetIndex; | ||
if (realIndex > 0) { | ||
return objectList.get(realIndex); | ||
} else { | ||
throw new JsonPathException("Target index:" + targetIndex + " larger than object count:" + objectList.size()); | ||
} | ||
} | ||
} | ||
throw new JsonPathException("Aggregation function attempted to calculate value using empty array"); | ||
} | ||
|
||
protected int getIndexFromParameters(EvaluationContext ctx, List<Parameter> parameters) { | ||
List<Number> numbers = Parameter.toList(Number.class, ctx, parameters); | ||
return numbers.get(0).intValue(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
json-path/src/main/java/com/jayway/jsonpath/internal/function/sequence/First.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.jayway.jsonpath.internal.function.sequence; | ||
|
||
import com.jayway.jsonpath.internal.EvaluationContext; | ||
import com.jayway.jsonpath.internal.function.Parameter; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Take the first item from collection of JSONArray | ||
* | ||
* Created by git9527 on 6/11/22. | ||
*/ | ||
public class First extends AbstractSequenceAggregation { | ||
@Override | ||
protected int targetIndex(EvaluationContext ctx, List<Parameter> parameters) { | ||
return 0; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
json-path/src/main/java/com/jayway/jsonpath/internal/function/sequence/Index.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.jayway.jsonpath.internal.function.sequence; | ||
|
||
import com.jayway.jsonpath.internal.EvaluationContext; | ||
import com.jayway.jsonpath.internal.function.Parameter; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Take the index from first Parameter, then the item from collection of JSONArray by index | ||
* | ||
* Created by git9527 on 6/11/22. | ||
*/ | ||
public class Index extends AbstractSequenceAggregation { | ||
@Override | ||
protected int targetIndex(EvaluationContext ctx, List<Parameter> parameters) { | ||
return getIndexFromParameters(ctx, parameters); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
json-path/src/main/java/com/jayway/jsonpath/internal/function/sequence/Last.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.jayway.jsonpath.internal.function.sequence; | ||
|
||
import com.jayway.jsonpath.internal.EvaluationContext; | ||
import com.jayway.jsonpath.internal.function.Parameter; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Take the first item from collection of JSONArray | ||
* | ||
* Created by git9527 on 6/11/22. | ||
*/ | ||
public class Last extends AbstractSequenceAggregation { | ||
@Override | ||
protected int targetIndex(EvaluationContext ctx, List<Parameter> parameters) { | ||
return -1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...-path/src/test/java/com/jayway/jsonpath/internal/function/SequentialPathFunctionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.jayway.jsonpath.internal.function; | ||
|
||
import com.jayway.jsonpath.Configuration; | ||
import com.jayway.jsonpath.Configurations; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Test cases for functions | ||
* | ||
* -first | ||
* -last | ||
* -index(X) | ||
* | ||
* Created by git9527 on 6/11/22. | ||
*/ | ||
public class SequentialPathFunctionTest extends BaseFunctionTest { | ||
|
||
private Configuration conf = Configurations.JACKSON_CONFIGURATION; | ||
|
||
@Test | ||
public void testFirstOfNumbers() throws Exception { | ||
verifyFunction(conf, "$.numbers.first()", NUMBER_SERIES, 1); | ||
} | ||
|
||
@Test | ||
public void testLastOfNumbers() throws Exception { | ||
verifyFunction(conf, "$.numbers.last()", NUMBER_SERIES, 10); | ||
} | ||
|
||
@Test | ||
public void testIndexOfNumbers() throws Exception { | ||
verifyFunction(conf, "$.numbers.index(0)", NUMBER_SERIES, 1); | ||
verifyFunction(conf, "$.numbers.index(-1)", NUMBER_SERIES, 10); | ||
verifyFunction(conf, "$.numbers.index(1)", NUMBER_SERIES, 2); | ||
} | ||
|
||
@Test | ||
public void testFirstOfText() throws Exception { | ||
verifyFunction(conf, "$.text.first()", TEXT_SERIES, "a"); | ||
} | ||
|
||
@Test | ||
public void testLastOfText() throws Exception { | ||
verifyFunction(conf, "$.text.last()", TEXT_SERIES, "f"); | ||
} | ||
|
||
@Test | ||
public void testIndexOfText() throws Exception { | ||
verifyFunction(conf, "$.text.index(0)", TEXT_SERIES, "a"); | ||
verifyFunction(conf, "$.text.index(-1)", TEXT_SERIES, "f"); | ||
verifyFunction(conf, "$.text.index(1)", TEXT_SERIES, "b"); | ||
} | ||
} |