diff --git a/common/src/main/java/dev/latvian/mods/kubejs/util/JsonIO.java b/common/src/main/java/dev/latvian/mods/kubejs/util/JsonIO.java index 7bbf5ba62..49aad74d0 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/util/JsonIO.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/util/JsonIO.java @@ -6,6 +6,7 @@ import com.google.gson.JsonElement; import com.google.gson.JsonNull; import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import com.google.gson.JsonPrimitive; import com.google.gson.JsonSyntaxException; import com.google.gson.internal.Streams; @@ -101,27 +102,25 @@ public static Object toPrimitive(@Nullable JsonElement element) { return null; } - @Nullable - public static Map read(Path path) throws IOException { - if (Files.notExists(path)) { + public static JsonElement readJson(Path path) throws IOException { + if (!Files.isRegularFile(path)) { return null; } try (var fileReader = Files.newBufferedReader(path)) { - var jsonReader = new JsonReader(fileReader); - JsonElement element; - var lenient = jsonReader.isLenient(); - jsonReader.setLenient(true); - element = Streams.parse(jsonReader); - - if (!element.isJsonNull() && jsonReader.peek() != JsonToken.END_DOCUMENT) { - throw new JsonSyntaxException("Did not consume the entire document."); - } - - return MapJS.of(element); + return JsonParser.parseReader(fileReader); } } + public static String readString(Path path) throws IOException { + return toString(readJson(path)); + } + + @Nullable + public static Map read(Path path) throws IOException { + return MapJS.of(readJson(path)); + } + public static void write(Path path, @Nullable JsonObject json) throws IOException { if (json == null || json.isJsonNull()) { Files.deleteIfExists(path); diff --git a/common/src/main/java/dev/latvian/mods/kubejs/util/NBTIOWrapper.java b/common/src/main/java/dev/latvian/mods/kubejs/util/NBTIOWrapper.java index a5692855a..b750a6feb 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/util/NBTIOWrapper.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/util/NBTIOWrapper.java @@ -11,7 +11,7 @@ public interface NBTIOWrapper { @Nullable static CompoundTag read(Path path) throws IOException { - if (Files.notExists(path)) { + if (!Files.isRegularFile(path)) { return null; }