-3

I want to be able to save my game, so I packed the information into a object(class) named GameController. How can I convert the information written in the JSON file back into my GameController object?

My saving code:

public static void Save(GameController controller) {
    JSONObject obj = new JSONObject();
    obj.put("controller", controller);
    try(FileWriter fileWriter = new FileWriter("saved.json")) {
        fileWriter.write(obj.toString());
        fileWriter.flush();;
    }
    catch(IOException e) { e.printStackTrace(); }
}

Upd: Friends online kindly suggested a related problem, but I don't think it did solve my problem. The problem of mine is my JSON file turned out to be like this:

{"controller":xyz.chengzi.aeroplanechess.controller.GameController@5c30107a}

And this JSON file doesn't allow phrasing, so I don't even know how to turn it into a JSON object. Does anybody know how to deal with this or did I save my file in a wrong way?

Shuxin
  • 17
  • 4
  • You can save and read the objects using serialization and deserialization. – Sunny Dec 07 '20 at 07:49
  • 2
    Please add code as text, not an image – OneCricketeer Dec 07 '20 at 07:51
  • Jackson (and maybe Gson) have file reading and writing capabilities builtin, by the way – OneCricketeer Dec 07 '20 at 07:52
  • Thanks so much for all your kind replies! I'm a new user and very sorry for the inconvenience I made for you. – Shuxin Dec 07 '20 at 08:22
  • I think obj.put() wants a string as a second parameter. You provider your gamecontroller object. So .toString() gets called on that object to transform it to a string. .toString() just returns the classname of the object its called on. Thats what happening there.. You still have to serialize your object, to make the string represent your object itself. – Olli Dec 07 '20 at 09:02
  • Thake a look at this https://www.tutorialspoint.com/how-to-convert-java-object-to-json-using-jackson-library link. This might help you – Olli Dec 07 '20 at 09:03

1 Answers1

0

Whats wrong?

Your problem is, that you did not serialize your object.

Take a look at how you try to serialize your object.

obj.put("controller", controller);

As a second parameter you provide an instance of your gamecontroller class. Since the .put() function wants a string as a second parameter, the .toString() function gets called on your controller instace.

If you don't override the toString function of your own classes, it will just return the classname.

So the put functions creates a property with the name "controller" and writes the classname of your parameter as its content.

Solution

You have to serialize your object. That will turn your object into a string, which represents the instance of your obejct. So the string contains all properties of your instace.

Here is an example how you can do this with Jackson Api.

You basically create an instance of an object mapper class and transform your controller instance with this object to a json string.

ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(controller);
fileWriter.write(jsonString);
Olli
  • 658
  • 5
  • 26
  • Thank you so much! Such a detailed and useful reply:) – Shuxin Dec 07 '20 at 11:47
  • I'm glad to help you. If this answere was helpful, think about marking it as the correct answere. For future questions you should definitely try to search for your problem here on this site before asking your question. – Olli Dec 07 '20 at 12:26