Fixed Serialization not working where there was an JSON read exception, and fixed the Serialisation not working for Routes
This commit is contained in:
@@ -47,6 +47,9 @@ public class Main extends Application {
|
|||||||
this.session.setDataManager(dataManager);
|
this.session.setDataManager(dataManager);
|
||||||
} catch (IOException e){
|
} catch (IOException e){
|
||||||
|
|
||||||
|
} catch (Exception e){
|
||||||
|
//this exception is for when the files is formatted wrong.
|
||||||
|
this.session.setDataManager(new DataManager());
|
||||||
}
|
}
|
||||||
|
|
||||||
//set up stage
|
//set up stage
|
||||||
|
|||||||
@@ -3,19 +3,30 @@ package model;
|
|||||||
import javafx.collections.FXCollections;
|
import javafx.collections.FXCollections;
|
||||||
import javafx.collections.ObservableList;
|
import javafx.collections.ObservableList;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by Gondr on 5/04/2017.
|
* Created by Gondr on 5/04/2017.
|
||||||
*/
|
*/
|
||||||
public class Route {
|
public class Route {
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
private ObservableList<Stop> stops;
|
private transient ObservableList<Stop> stops;
|
||||||
|
private ArrayList<Stop> serialisedStops;
|
||||||
|
|
||||||
public Route(String name, ObservableList<Stop> stops){
|
public Route(String name, ObservableList<Stop> stops){
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.stops = FXCollections.observableArrayList(stops);
|
this.stops = FXCollections.observableArrayList(stops);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void serialise(){
|
||||||
|
serialisedStops = new ArrayList<>(stops);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deserialise(){
|
||||||
|
stops = FXCollections.observableArrayList(serialisedStops);
|
||||||
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,10 +6,7 @@ import model.Ride;
|
|||||||
import model.Route;
|
import model.Route;
|
||||||
import model.Stop;
|
import model.Stop;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.*;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.Reader;
|
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
@@ -36,6 +33,22 @@ public class DataManagerSerialiser extends Serialiser {
|
|||||||
return (serialRidesSuccess && serialStopsSuccess && serialRoutesSuccess);
|
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 {
|
public DataManager load() throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
|
||||||
Reader reader = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("serialisation/rides.json"), "UTF-8");
|
Reader reader = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("serialisation/rides.json"), "UTF-8");
|
||||||
ArrayList<Ride> rides = gson.fromJson(reader, new TypeToken<ArrayList<Ride>>(){}.getType());
|
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());
|
ArrayList<Route> routes = gson.fromJson(reader3, new TypeToken<ArrayList<Route>>(){}.getType());
|
||||||
reader3.close();
|
reader3.close();
|
||||||
|
|
||||||
|
for (Route route: routes){
|
||||||
|
route.deserialise();
|
||||||
|
}
|
||||||
|
|
||||||
// Constructor c = DataManager.class.getDeclaredConstructor(List.class, List.class, List.class);
|
// Constructor c = DataManager.class.getDeclaredConstructor(List.class, List.class, List.class);
|
||||||
// DataManager dataManager = (DataManager) c.newInstance(rides, stops, routes);
|
// DataManager dataManager = (DataManager) c.newInstance(rides, stops, routes);
|
||||||
// return dataManager;
|
// return dataManager;
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
[]
|
|
||||||
Binary file not shown.
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"}]
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user