diff options
Diffstat (limited to 'src/main/java/io/github/moulberry/notenoughupdates/util/JsonUtils.java')
| -rw-r--r-- | src/main/java/io/github/moulberry/notenoughupdates/util/JsonUtils.java | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/JsonUtils.java b/src/main/java/io/github/moulberry/notenoughupdates/util/JsonUtils.java new file mode 100644 index 00000000..916631b7 --- /dev/null +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/JsonUtils.java @@ -0,0 +1,42 @@ +package io.github.moulberry.notenoughupdates.util; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; + +import java.util.Collections; +import java.util.List; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + +public class JsonUtils { + public static Stream<JsonElement> getJsonArrayAsStream(JsonArray array) { + return StreamSupport.stream(array.spliterator(), false); + } + + public static <T> List<T> transformJsonArrayToList(JsonArray array, Function<? super JsonElement, ? extends T> mapper) { + return getJsonArrayAsStream(array).map(mapper).collect(Collectors.toList()); + } + + public static <T> List<T> getJsonArrayOrEmpty(JsonObject rootObject, String name, Function<? super JsonElement, ? extends T> mapper) { + if (!rootObject.has(name)) { + return Collections.emptyList(); + } + JsonElement jsonElement = rootObject.get(name); + if (jsonElement.isJsonArray()) { + return transformJsonArrayToList(jsonElement.getAsJsonArray(), mapper); + } + return Collections.emptyList(); + } + + public static <T> JsonArray transformListToJsonArray(List<T> things, Function<? super T, ? extends JsonElement> mapper) { + JsonArray array = new JsonArray(); + for (T t : things) { + array.add(mapper.apply(t)); + } + return array; + } + +} |
