From 9adc51f8d74577b5c4ae9c1f88341252f2c22939 Mon Sep 17 00:00:00 2001 From: sowgro Date: Thu, 13 Nov 2025 20:05:25 -0500 Subject: export almost working --- .../persistence/importexport/JSONHandler.java | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/main/java/design/persistence/importexport/JSONHandler.java (limited to 'src/main/java/design/persistence/importexport/JSONHandler.java') 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); + } +} + + -- cgit v1.2.3 From cf59d52cba70742f1d4098c38b4c7a798b3d89fa Mon Sep 17 00:00:00 2001 From: sowgro Date: Sun, 16 Nov 2025 01:56:27 -0500 Subject: IMPORT-EXPORT WORKING!!!!!!!!!!!!!!!!!!!!!!!!!! --- .../design/persistence/importexport/JSONHandler.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'src/main/java/design/persistence/importexport/JSONHandler.java') diff --git a/src/main/java/design/persistence/importexport/JSONHandler.java b/src/main/java/design/persistence/importexport/JSONHandler.java index 239eb6b..9c04281 100644 --- a/src/main/java/design/persistence/importexport/JSONHandler.java +++ b/src/main/java/design/persistence/importexport/JSONHandler.java @@ -1,6 +1,7 @@ package design.persistence.importexport; -import com.fasterxml.jackson.databind.JsonNode; +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; @@ -8,22 +9,26 @@ import java.io.IOException; public class JSONHandler implements DataHandler { private final DataSource dataSource; - private final JsonMapper jsonMapper = new JsonMapper(); + 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 { - JsonNode tree = jsonMapper.readTree(file); - dataSource.importData(tree); + Object data = jsonMapper.readValue(file, dataSource.getTargetClass()); + dataSource.importData(data); } @Override public void exportData(File file) throws IOException{ - JsonNode tree = dataSource.exportData(); - jsonMapper.writerWithDefaultPrettyPrinter().writeValue(file, tree); + Object data = dataSource.exportData(); + jsonMapper.writer(new Serializers.CustomPrettyPrinter()) + .writeValue(file, data); } } -- cgit v1.2.3