forked from reposense/RepoSense
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonParser.java
More file actions
36 lines (29 loc) · 1.04 KB
/
JsonParser.java
File metadata and controls
36 lines (29 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package reposense.parser;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.file.Path;
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
/**
* Represents a {@code JsonParser} that is able to parse json file from a {@code Path} into an object of type {@code T}.
*/
public abstract class JsonParser<T> {
/**
* Gets the type of {@code T} for json conversion.
*/
public abstract Type getType();
/**
* Converts json file from the given {@code path} into an object and returns it.
* @throws IOException if {@code path} is invalid.
*/
public abstract T parse(Path path) throws IOException;
protected T fromJson(Path path) throws IOException {
return fromJson(new Gson(), path, getType());
}
protected T fromJson(Gson gson, Path path, Type type) throws IOException {
try (JsonReader jsonReader = new JsonReader(new FileReader(path.toString()))) {
return gson.fromJson(jsonReader, type);
}
}
}