-
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 unique items from collection of JSONArray
- Loading branch information
1 parent
21de620
commit eadcd79
Showing
5 changed files
with
56 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
31 changes: 31 additions & 0 deletions
31
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,31 @@ | ||
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.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** | ||
* Take the unique items from collection of JSONArray | ||
* | ||
* 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); | ||
Set<Object> objectSet = new HashSet<>(); | ||
objects.forEach(objectSet::add); | ||
|
||
return new ArrayList<>(objectSet); | ||
} | ||
throw new JsonPathException("Aggregation function attempted to calculate value using empty 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