diff options
Diffstat (limited to 'src/main/java/design/persistence/importexport/JSONHandler.java')
| -rw-r--r-- | src/main/java/design/persistence/importexport/JSONHandler.java | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/main/java/design/persistence/importexport/JSONHandler.java b/src/main/java/design/persistence/importexport/JSONHandler.java new file mode 100644 index 0000000..239eb6b --- /dev/null +++ b/src/main/java/design/persistence/importexport/JSONHandler.java @@ -0,0 +1,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); + } +} + + |
