Skip to content

Commit fd7d9d4

Browse files
author
Gonzalo Diaz
committed
[FEATURE] New utility to encapsulate JSON loading.
1 parent 306a079 commit fd7d9d4

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package util;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.util.List;
7+
8+
/**
9+
* JsonLoader.
10+
*/
11+
public final class JsonLoader {
12+
13+
private JsonLoader() {}
14+
15+
/**
16+
* loadJson.
17+
*/
18+
public static <T> List<T> loadJson(String path, Class<T> type) throws IOException {
19+
ObjectMapper objectMapper = new ObjectMapper();
20+
21+
File file = new File(
22+
JsonLoader.class.getClassLoader()
23+
.getResource(path)
24+
.getFile()
25+
);
26+
27+
ObjectMapper mapper = new ObjectMapper();
28+
return mapper.readerForListOf(type)
29+
.readValue(objectMapper.readTree(file));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)