-1

I am working on a project in Java where the user must be able to create a Kanban board, (basically Trello) if you have used one before. So at the current time, we have reached a point where we can create a Kanban board in the program. The next task is to be able to save the board to a file and load it back. I have heard that JSON could be a good solution for this.

I wanted to know whether anyone agreed with this or if they had any other solutions. The Kanban board object will have an ID and an Array of columns on the board, each column will have an array of cards for each column. I think that saving this should be fairly straight forward if using a JSON, but I wanted to know how you guys would go about loading the board back into the program. Of course, there are methods in place that add cards, columns etc.

LondonMassive
  • 367
  • 2
  • 5
  • 20
  • save the state of the object. later on, read those data, and pass them to the constructor, a Builder, a Mapper, ... – Stultuske Nov 28 '19 at 11:27
  • When I save the object to a JSON, will the data in the Arrays be kept? What I am trying to understand, is if I save an Array to a JSON, and then I try to read back from the JSON, how will accessing the Array work. – LondonMassive Nov 28 '19 at 11:51
  • Read on serialization, either Java object serialization, JSON serialization or XML serialization. The first will result in a binary file, while the latter two will result in a file that it's both human and machine readable and can handle schema changes with a bit more flexibility. All of them are suitable for handling complex objects. – ggf31416 Nov 28 '19 at 12:04

2 Answers2

0

Json would be a good idea, because you can store complex objects in it. You could use the Gson Library from Google for Java to convert Java objects to Json and store it and the opposite, read the Json from a file and convert it to Java objects.

Another method would be to use a ObjectOutputStream to store and read the objects in a file.

Still, I would prefer to use Json, because I think it is easier to use and you have a better overview, because you still can read the Json, whereas the ObjectOutputstream will encode it (if I remember correctly).

You can check here for examples: https://www.mkyong.com/java/how-to-write-an-object-to-file-in-java/

If there any more questions, let us know.

bbrinck
  • 953
  • 1
  • 9
  • 33
0

XML might even be better. With JAXB and some annotations in the DOM classes.

Very simple, but okay a tutorial.

public static Definitions loadModel(Path path) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(Definitions.class);
    Unmarshaller um = context.createUnmarshaller();
    Definitions definitions = (Definitions) um.unmarshal(path.toFile());
    return definitions;
}

@XmlRootElement
public class Definitions {

    @XmlAttribute(name = "subject")
    public String subject;

    @XmlElement(name="field"),
    public List<Field> items;
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138