summaryrefslogtreecommitdiff
path: root/src/main/java/design/persistence/importexport/JSONHandler.java
blob: 239eb6b02726ab8f574948765a904341439d602d (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
package design.persistence.importexport;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.json.JsonMapper;

import java.io.File;
import java.io.IOException;

public class JSONHandler implements DataHandler {

    private final DataSource dataSource;
    private final JsonMapper jsonMapper = new JsonMapper();

    public JSONHandler(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public void importData(File file) throws IOException {
        JsonNode tree = jsonMapper.readTree(file);
        dataSource.importData(tree);
    }

    @Override
    public void exportData(File file) throws IOException{
        JsonNode tree = dataSource.exportData();
        jsonMapper.writerWithDefaultPrettyPrinter().writeValue(file, tree);
    }
}