Skip to content

Commit b8a91a3

Browse files
committed
Prioritize long over double when reading JSON
1 parent 8fc040a commit b8a91a3

File tree

1 file changed

+17
-0
lines changed
  • json-data-utils/src/main/java/net/minecraftforge/util/data/json

1 file changed

+17
-0
lines changed

json-data-utils/src/main/java/net/minecraftforge/util/data/json/JsonData.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import com.google.gson.GsonBuilder;
99
import com.google.gson.JsonIOException;
1010
import com.google.gson.JsonSyntaxException;
11+
import com.google.gson.ToNumberPolicy;
1112
import com.google.gson.reflect.TypeToken;
13+
import com.google.gson.stream.JsonReader;
1214

1315
import java.io.ByteArrayInputStream;
1416
import java.io.File;
@@ -21,9 +23,24 @@
2123
// TODO [MCMaven][Documentation] Document
2224
public class JsonData {
2325
private static final Gson GSON = new GsonBuilder()
26+
.setObjectToNumberStrategy(JsonData::readNumber)
2427
.setPrettyPrinting()
2528
.create();
2629

30+
private static Number readNumber(JsonReader in) throws IOException {
31+
try {
32+
return ToNumberPolicy.LONG_OR_DOUBLE.readNumber(in);
33+
} catch (Throwable suppressed) {
34+
try {
35+
return ToNumberPolicy.BIG_DECIMAL.readNumber(in);
36+
} catch (Throwable e) {
37+
IOException throwing = new IOException("Failed to read number from " + in, e);
38+
throwing.addSuppressed(suppressed);
39+
throw throwing;
40+
}
41+
}
42+
}
43+
2744
public static AssetsIndex assetsIndex(File file) {
2845
return fromJson(file, AssetsIndex.class);
2946
}

0 commit comments

Comments
 (0)