Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1902' into 2002
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxNeedsSnacks committed Feb 18, 2024
2 parents f67dee9 + 4d4a952 commit 49b6dd2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
27 changes: 13 additions & 14 deletions common/src/main/java/dev/latvian/mods/kubejs/util/JsonIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 49b6dd2

Please sign in to comment.