forked from json-path/JsonPath
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
distinct
JsonPath function (#3)
Co-authored-by: PavelSakharchuk <[email protected]> Co-authored-by: Valery Yatsynovich <[email protected]>
- Loading branch information
1 parent
12609d9
commit fd1bbfe
Showing
5 changed files
with
97 additions
and
15 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
28 changes: 28 additions & 0 deletions
28
json-path/src/main/java/com/jayway/jsonpath/internal/function/sequence/Distinct.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,28 @@ | ||
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.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.StreamSupport; | ||
|
||
/** | ||
* Take the unique items from collection of JSONArray | ||
*/ | ||
public class Distinct implements PathFunction { | ||
|
||
@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); | ||
return StreamSupport.stream(objects.spliterator(), false) | ||
.distinct() | ||
.collect(Collectors.toList()); | ||
} | ||
throw new JsonPathException("Aggregation function attempted unique values using non array"); | ||
} | ||
} |
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