| 1 | package net.minecraft.util.eventlog;
|
| 2 |
|
| 3 | import com.google.gson.JsonElement;
|
| 4 | import com.google.gson.JsonParseException;
|
| 5 | import com.google.gson.JsonParser;
|
| 6 | import com.google.gson.Strictness;
|
| 7 | import com.google.gson.stream.JsonReader;
|
| 8 | import com.mojang.serialization.Codec;
|
| 9 | import com.mojang.serialization.JsonOps;
|
| 10 | import java.io.Closeable;
|
| 11 | import java.io.EOFException;
|
| 12 | import java.io.IOException;
|
| 13 | import java.io.Reader;
|
| 14 | import org.jspecify.annotations.Nullable;
|
| 15 |
|
| 16 | public interface JsonEventLogReader<T> extends Closeable {
|
| 17 | static <T> JsonEventLogReader<T> create(final Codec<T> codec, final Reader reader) {
|
| 18 | final JsonReader jsonReader = new JsonReader(reader);
|
| 19 | jsonReader.setStrictness(Strictness.LENIENT);
|
| 20 | return new JsonEventLogReader<T>() {
|
| 21 | @Override
|
| 22 | public @Nullable T next() throws IOException {
|
| 23 | try {
|
| 24 | if (!jsonReader.hasNext()) {
|
| 25 | return null;
|
| 26 | }
|
| 27 |
|
| 28 | JsonElement json = JsonParser.parseReader(jsonReader);
|
| 29 | return codec.parse(JsonOps.INSTANCE, json).getOrThrow(IOException::new);
|
| 30 | } catch (JsonParseException e) {
|
| 31 | throw new IOException(e);
|
| 32 | } catch (EOFException e) {
|
| 33 | return null;
|
| 34 | }
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | public void close() throws IOException {
|
| 39 | jsonReader.close();
|
| 40 | }
|
| 41 | };
|
| 42 | }
|
| 43 |
|
| 44 | @Nullable T next() throws IOException;
|
| 45 | }
|
| 46 |
|