From cdc24f87a2a6485480abd28372cf946c5e36baff Mon Sep 17 00:00:00 2001 From: Kenneth van Ewijk Date: Thu, 17 Dec 2015 00:22:28 +0100 Subject: [PATCH] Parse JSON file --- NavCityBreda/Helpers/JSONParser.cs | 126 +++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 NavCityBreda/Helpers/JSONParser.cs diff --git a/NavCityBreda/Helpers/JSONParser.cs b/NavCityBreda/Helpers/JSONParser.cs new file mode 100644 index 0000000..7a37571 --- /dev/null +++ b/NavCityBreda/Helpers/JSONParser.cs @@ -0,0 +1,126 @@ +using NavCityBreda.Model; +using System; +using System.Collections.Generic; +using System.IO; +using Newtonsoft.Json.Linq; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NavCityBreda.Helpers +{ + class JSONParser + { + public static Route LoadRoute(string file) + { + if (!File.Exists(file)) + throw new FileNotFoundException("The file does not exist: " + file); + + string json = File.ReadAllText(file); + JObject o = JObject.Parse(json); + + string error; + if (!ValidateRouteObject(o, out error)) + throw new FileLoadException("Invalid Route information in " + file + ", " + error); + + Route r = new Route((string)o["name"], (string)o["description"], (string)o["landmarks"], Int32.Parse((string)o["minutes"])); + + JToken[] waypoints = o["waypoints"].ToArray(); + foreach(JToken t in waypoints) + { + if(!ValidateWaypointObject(t, out error)) + throw new FileLoadException("Invalid Waypoint information in " + file + ", " + error); + + Waypoint w; + + if ((bool)t["landmark"]) + w = new Landmark((double)t["latitude"], (double)t["longitude"], (string)t["name"], (string)o["description"], t["image"].NullOrEmpty() ? "" : (string)t["image"]); + else + w = new Waypoint((double)t["latitude"], (double)t["longitude"], (string)t["name"]); + + r.Add(w); + } + + return r; + } + + private static bool ValidateRouteObject(JObject o, out string error) + { + bool valid = true; + error = "Success"; + + if (o["name"].NullOrEmpty()) + { + valid = false; + error = "Name missing"; + } + + if (o["description"].NullOrEmpty()) + { + valid = false; + error = "Description missing"; + } + + if (o["landmarks"].NullOrEmpty()) + { + valid = false; + error = "Landmarks missing"; + } + + if (o["minutes"].NullOrEmpty()) + { + valid = false; + error = "Minutes missing"; + } + + if (o["waypoints"].NullOrEmpty()) + { + valid = false; + error = "Waypoints missing or empty"; + } + + return valid; + } + + private static bool ValidateWaypointObject(JToken o, out string error) + { + bool valid = true; + error = "Success"; + + if (o["name"].NullOrEmpty()) + { + valid = false; + error = "Name missing"; + } + + if (o["landmark"].NullOrEmpty()) + { + valid = false; + error = "Landmark missing"; + } + + if (o["latitude"].NullOrEmpty()) + { + valid = false; + error = "Latitude missing"; + } + + if (o["longitude"].NullOrEmpty()) + { + valid = false; + error = "Longitude missing"; + } + + if( valid && (bool)o["landmark"]) + { + if (o["description"].NullOrEmpty()) + { + valid = false; + error = "Description missing"; + } + } + + return valid; + } + } +}