I been working on this for way too long, I think I am just doing something stupid with GSON, not really sure. My debug indicates that it makes it to the map = builder.fromJson method but it still returns null.
Saving and Loading Methods
public static void saveTeleportData() {
Path path = Paths.get("./data/definitions/teleports.json");
File file = path.toFile();
file.getParentFile().setWritable(true);
if (!file.getParentFile().exists()) {
try {
file.getParentFile().mkdirs();
} catch (SecurityException e) {
System.out.println("Unable to create directory for Teleport data!");
}
}
try (FileWriter writer = new FileWriter(file)) {
Gson builder = new GsonBuilder().setPrettyPrinting().create();
JsonObject object = new JsonObject();
object.add("teleport-load", builder
.toJsonTree(***Teleport Object Array***));
writer.write(builder.toJson(object));
writer.close();
} catch (Exception e) {
System.out.println("Failure");
}
}
The saving method outputs this
So this looks right to me.
Heres the method that is called to load all of the 'Teleport' objects
public static Teleport[] loadTeleports() {
// Create the path and file objects.
Path path = Paths.get("./data/definitions/teleports.json");
File file = path.toFile();
// Now read the properties from the json parser.
Teleport[] map = null;
try (FileReader fileReader = new FileReader(file)) {
JsonParser fileParser = new JsonParser();
Gson builder = new GsonBuilder().create();
JsonObject reader = (JsonObject) fileParser.parse(fileReader);
if (reader.has("teleport-load")) {
map = builder.fromJson("teleport-load", Teleport[].class);
return map;
}
} catch (Exception e) {
return null;
}
return map;
}
Also here is my Teleport Class, I am not sure what I could be doing wrong, all advice is appreciated! Let me know if you have any questions!
public class Teleport {
private final String name;
private final Position position;
private final TeleportGroups teleGroup;
public Teleport(String name, Position position, TeleportGroups teleGroup) {
this.name = name;
this.position = position;
this.teleGroup = teleGroup;
}
public String getName() {
return name;
}
public Position getPosition() {
return position;
}
public TeleportGroups getTeleType() {
return teleGroup;
}
}
TL;DR - My loadTeleports method returns null even after I know its reading the file. GSON