1

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

https://pastebin.com/D5VpJXAj

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

1 Answer 1

1

fromJson method expects a Reader, a JsonReader, a JsonElement, or a String with your actual JSON data as a first parameter, but you pass "teleport-load". Try to call it like this:

map = gson.fromJson(reader.get("teleport-load"), Teleport[].class);

In this case reader.get("teleport-load") will return a JsonElement representing the teleport-load property and then fromJson will convert it to a Teleport[] array.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Kirill! I used this same loading for other basic objects and it seemed to work, not sure why I had problems with this one. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.