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.
Defines the pattern for taking unique items from collection of JSONArray
- Loading branch information
1 parent
21de620
commit e707a46
Showing
5 changed files
with
99 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
30 changes: 30 additions & 0 deletions
30
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,30 @@ | ||
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 | ||
* <p> | ||
* Created by PavelSakharchuk on 21/09/23 | ||
*/ | ||
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