Utility for reading lists
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Johannes Frohnmeyer 2022-11-01 19:23:51 +01:00
parent 780e6a7283
commit 8d50990c1a
Signed by: Johannes
GPG Key ID: E76429612C2929F4
2 changed files with 27 additions and 0 deletions

View File

@ -12,6 +12,7 @@ repositories {
}
dependencies {
implementation("io.gitlab.jfronny:commons:2022.11.1+17-58-36")
api("io.gitlab.jfronny:commons-gson:2022.11.1+17-58-36")
}

View File

@ -0,0 +1,26 @@
package io.gitlab.jfronny.gson.compile.util;
import io.gitlab.jfronny.commons.throwable.ThrowingFunction;
import io.gitlab.jfronny.gson.stream.JsonReader;
import io.gitlab.jfronny.gson.stream.JsonToken;
import java.io.IOError;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
public class GList {
public static <T> List<T> read(JsonReader reader, ThrowingFunction<JsonReader, T, IOError> read) throws IOException {
if (reader.isLenient() && reader.peek() != JsonToken.BEGIN_ARRAY) return List.of(read.apply(reader));
reader.beginArray();
List<T> res = new LinkedList<>();
while (reader.hasNext()) {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
res.add(null);
} else res.add(read.apply(reader));
}
reader.endArray();
return res;
}
}