diff options
Diffstat (limited to 'src/main/java/design/persistence/JSONPersonalDatabase.java')
| -rw-r--r-- | src/main/java/design/persistence/JSONPersonalDatabase.java | 27 |
1 files changed, 12 insertions, 15 deletions
diff --git a/src/main/java/design/persistence/JSONPersonalDatabase.java b/src/main/java/design/persistence/JSONPersonalDatabase.java index 3fc5cab..3e70629 100644 --- a/src/main/java/design/persistence/JSONPersonalDatabase.java +++ b/src/main/java/design/persistence/JSONPersonalDatabase.java @@ -13,8 +13,6 @@ import design.model.League; import java.io.File; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.StandardCopyOption; import java.util.HashMap; import java.util.Map; @@ -38,9 +36,6 @@ public class JSONPersonalDatabase implements PersonalDatabase { this.cache = new HashMap<>(); this.mapper = new ObjectMapper(); - // TODO: Once the saved JSON matches the model, consider removing. - // TEMP: tolerate unknown props while the model stabilizes - mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); SimpleModule module = new SimpleModule(); module.addDeserializer(Course.class, new Serializers.CourseIdDeserializer()); @@ -48,6 +43,7 @@ public class JSONPersonalDatabase implements PersonalDatabase { module.addSerializer(League.class, new Serializers.LeagueIDSerializer()); module.addDeserializer(League.class, new Serializers.LeagueIDDeserializer()); mapper.registerModule(module); + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); mapper.registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES)); mapper.registerModule(new JavaTimeModule()); @@ -101,18 +97,19 @@ public class JSONPersonalDatabase implements PersonalDatabase { save(); } - - public File exportData(File newFile) throws IOException{ - Files.copy(file.toPath(), newFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - return newFile; - } - - public void importData(File newFile) throws IOException { - Golfer[] newGolfers = mapper.readValue(newFile, Golfer[].class); + @Override + public void importData(JsonNode tree) throws IOException { + Golfer[] data = mapper.treeToValue(tree, Golfer[].class); cache.clear(); - for (Golfer g : newGolfers) { - cache.put(g.getUsername(), g); + for (Golfer golfer : data) { + cache.put(golfer.getUsername(), golfer); } save(); } + + @Override + public JsonNode exportData() { + Object[] data = cache.values().toArray(); + return mapper.valueToTree(data); + } } |
