Skip to content

Commit 9231752

Browse files
committed
JsonData.fromJson with TypeToken parameter
1 parent 5114f6e commit 9231752

File tree

1 file changed

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

1 file changed

+18
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.google.gson.GsonBuilder;
99
import com.google.gson.JsonIOException;
1010
import com.google.gson.JsonSyntaxException;
11+
import com.google.gson.reflect.TypeToken;
1112

1213
import java.io.ByteArrayInputStream;
1314
import java.io.File;
@@ -146,6 +147,23 @@ public static <T> T fromJson(String data, Class<T> classOfT) throws JsonSyntaxEx
146147
return GSON.fromJson(data, classOfT);
147148
}
148149

150+
public static <T> T fromJson(File file, TypeToken<T> classOfT) throws JsonSyntaxException, JsonIOException {
151+
try (FileInputStream stream = new FileInputStream(file)) {
152+
return fromJson(stream, classOfT);
153+
} catch (IOException e) {
154+
throw new JsonIOException(e);
155+
}
156+
}
157+
public static <T> T fromJson(byte[] data, TypeToken<T> classOfT) throws JsonSyntaxException, JsonIOException {
158+
return fromJson(new ByteArrayInputStream(data), classOfT);
159+
}
160+
public static <T> T fromJson(InputStream stream, TypeToken<T> classOfT) throws JsonSyntaxException, JsonIOException {
161+
return GSON.fromJson(new InputStreamReader(stream), classOfT);
162+
}
163+
public static <T> T fromJson(String data, TypeToken<T> classOfT) throws JsonSyntaxException, JsonIOException {
164+
return GSON.fromJson(data, classOfT);
165+
}
166+
149167
public static String toJson(Object src) {
150168
return GSON.toJson(src);
151169
}

0 commit comments

Comments
 (0)