|
8 | 8 | import com.google.gson.GsonBuilder; |
9 | 9 | import com.google.gson.JsonIOException; |
10 | 10 | import com.google.gson.JsonSyntaxException; |
| 11 | +import com.google.gson.reflect.TypeToken; |
11 | 12 |
|
12 | 13 | import java.io.ByteArrayInputStream; |
13 | 14 | import java.io.File; |
@@ -146,6 +147,23 @@ public static <T> T fromJson(String data, Class<T> classOfT) throws JsonSyntaxEx |
146 | 147 | return GSON.fromJson(data, classOfT); |
147 | 148 | } |
148 | 149 |
|
| 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 | + |
149 | 167 | public static String toJson(Object src) { |
150 | 168 | return GSON.toJson(src); |
151 | 169 | } |
|
0 commit comments