-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJson2Files.java
42 lines (32 loc) · 1.29 KB
/
Json2Files.java
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
37
38
39
40
41
42
package de.bolben.utils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
/**
* @Author Benjamin Bolgrin
*
* The class Json2Files will take the nodes of a JsonNode object and save them to separate json files on the file system.
*
*/
public class Json2Files {
private static final ObjectMapper objectMapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
private final JsonNode jsonNode;
private final String fileNameKey;
private final Path rootDir;
public Json2Files(Path path /* Directory name */, JsonNode jsonNode, String fileNameKey /* Json key to use as file name */){
this.jsonNode = jsonNode;
this.fileNameKey = fileNameKey;
rootDir = path;
}
public void writeFiles()
throws IOException {
for(JsonNode id : jsonNode){
Path filePath = rootDir.resolve(objectMapper.writeValueAsString(id.get(fileNameKey)).replaceAll("[\"']", "") + ".json");
Files.writeString(filePath, objectMapper.writeValueAsString(id), StandardCharsets.UTF_8);
}
}
}