Fixed Serialization not working where there was an JSON read exception, and fixed the Serialisation not working for Routes

main
Fan-Wu Yang 9 years ago
parent 63ac196488
commit 6a4fb731d5

@ -47,6 +47,9 @@ public class Main extends Application {
this.session.setDataManager(dataManager);
} catch (IOException e){
} catch (Exception e){
//this exception is for when the files is formatted wrong.
this.session.setDataManager(new DataManager());
}
//set up stage

@ -3,19 +3,30 @@ package model;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.util.ArrayList;
/**
* Created by Gondr on 5/04/2017.
*/
public class Route {
private String name;
private ObservableList<Stop> stops;
private transient ObservableList<Stop> stops;
private ArrayList<Stop> serialisedStops;
public Route(String name, ObservableList<Stop> stops){
this.name = name;
this.stops = FXCollections.observableArrayList(stops);
}
public void serialise(){
serialisedStops = new ArrayList<>(stops);
}
public void deserialise(){
stops = FXCollections.observableArrayList(serialisedStops);
}
public String getName() {
return name;
}

@ -6,10 +6,7 @@ import model.Ride;
import model.Route;
import model.Stop;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
@ -36,6 +33,22 @@ public class DataManagerSerialiser extends Serialiser {
return (serialRidesSuccess && serialStopsSuccess && serialRoutesSuccess);
}
public boolean serialise(ArrayList<Route> routes, String filepath){
for (Route route: routes){
route.serialise();
}
try{
String path = getClass().getClassLoader().getResource(filepath).getPath();
Writer writer = new OutputStreamWriter(new FileOutputStream(path), "UTF-8");
gson.toJson(routes, writer);
writer.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public DataManager load() throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Reader reader = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("serialisation/rides.json"), "UTF-8");
ArrayList<Ride> rides = gson.fromJson(reader, new TypeToken<ArrayList<Ride>>(){}.getType());
@ -49,6 +62,10 @@ public class DataManagerSerialiser extends Serialiser {
ArrayList<Route> routes = gson.fromJson(reader3, new TypeToken<ArrayList<Route>>(){}.getType());
reader3.close();
for (Route route: routes){
route.deserialise();
}
// Constructor c = DataManager.class.getDeclaredConstructor(List.class, List.class, List.class);
// DataManager dataManager = (DataManager) c.newInstance(rides, stops, routes);
// return dataManager;

Binary file not shown.

@ -1 +1 @@
[]
[{"name":"University to Dorms","serialisedStops":[{"address":"1 University Drive"},{"address":"1 Homestead Lane"}]}]

@ -1 +1 @@
[{"address":"a"},{"address":"b"}]
[{"address":"1 University Drive"},{"address":"1 Homestead Lane"}]
Loading…
Cancel
Save