blob: 9c042815cd5e0d5966004861a7358fa5d595c38c (
plain) (
blame)
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
|
package design.persistence.importexport;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import design.persistence.Serializers;
import java.io.File;
import java.io.IOException;
public class JSONHandler implements DataHandler {
private final DataSource dataSource;
private final ObjectMapper jsonMapper = new JsonMapper();
public JSONHandler(DataSource dataSource) {
this.dataSource = dataSource;
Serializers.configureMapper(jsonMapper);
jsonMapper.registerModule(dataSource.getJacksonModule());
}
@Override
public void importData(File file) throws IOException {
Object data = jsonMapper.readValue(file, dataSource.getTargetClass());
dataSource.importData(data);
}
@Override
public void exportData(File file) throws IOException{
Object data = dataSource.exportData();
jsonMapper.writer(new Serializers.CustomPrettyPrinter())
.writeValue(file, data);
}
}
|