/// <summary> /// Override method to serialize a WTTTimeTableCollection object to a JSON string /// </summary> /// <returns></returns> public override void WriteJson(JsonWriter Writer, object Value, JsonSerializer Serializer) { //Validate Arguments if (Serializer == null) { throw new ArgumentNullException(ExceptionHelper.GetStaticException("GeneralNullArgument", new string[] { "Serializer" }, new System.Globalization.CultureInfo("en-GB"))); } if (Writer == null) { throw new ArgumentNullException(ExceptionHelper.GetStaticException("GeneralNullArgument", new string[] { "Writer" }, new System.Globalization.CultureInfo("en-GB"))); } WTTTimeTableCollection TimeTableCollection = (WTTTimeTableCollection)Value; //Create the surrogate and serialize it instead of the collection itself if (TimeTableCollection != null) { WTTTimeTableCollectionSurrogate SurrogateTimeTableCollection = new WTTTimeTableCollectionSurrogate() { TimeTables = TimeTableCollection.ToList(), StartDate = TimeTableCollection.StartDate }; Serializer.Serialize(Writer, SurrogateTimeTableCollection); } }
/// <summary> /// Override method to deserialize a JSON string into a WTTTimeTableCollection /// </summary> /// <returns></returns> public override object ReadJson(JsonReader Reader, Type ObjectType, object ExistingValue, JsonSerializer Serializer) { //Validate Arguments if (Serializer == null) { throw new ArgumentNullException(ExceptionHelper.GetStaticException("GeneralNullArgument", new string[] { "Serializer" }, new System.Globalization.CultureInfo("en-GB"))); } if (Reader == null) { throw new ArgumentNullException(ExceptionHelper.GetStaticException("GeneralNullArgument", new string[] { "Reader" }, new System.Globalization.CultureInfo("en-GB"))); } //Deserialize reader into surrogate object WTTTimeTableCollectionSurrogate SurrogateTimeTableCollection = Serializer.Deserialize <WTTTimeTableCollectionSurrogate>(Reader); //Extract properties List <WTTTimeTable> TimeTables = SurrogateTimeTableCollection.TimeTables; //Instantiate new WTTTimeTableCollection WTTTimeTableCollection TimeTableCollection = new WTTTimeTableCollection(SurrogateTimeTableCollection.StartDate); //Populate the timetables and reseed WTTTripCollection Start Date foreach (WTTTimeTable TimeTable in TimeTables) { WTTTripCollection UpdatedTripCollection = new WTTTripCollection(TimeTable.StartDate); foreach (WTTTrip Trip in TimeTable.Trip) { UpdatedTripCollection.Add(Trip); } TimeTable.Trip = UpdatedTripCollection; TimeTableCollection.Add(TimeTable); } return(TimeTableCollection); }
public void WTTTimeTableCollection_Constuctor_XElement() { string FullPath = new Uri($@"{AppDomain.CurrentDomain.BaseDirectory}Resources\TestWTT_4.8.xml").LocalPath; XElement TestXML = XDocument.Load(FullPath).Element("SimSigTimetable").Element("Timetables"); GroundFrame.Core.Timetables.WTTTimeTableCollection TestTimeTableCollection = new GroundFrame.Core.Timetables.WTTTimeTableCollection(TestXML, new DateTime(2018, 7, 1)); Assert.Equal(205, TestTimeTableCollection.Count); }
public void WTTTimeTableCollection_Method_GetMapperLocationNodes() { string FullPath = new Uri($@"{AppDomain.CurrentDomain.BaseDirectory}Resources\TestWTT_4.8.xml").LocalPath; XElement TestXML = XDocument.Load(FullPath).Element("SimSigTimetable").Element("Timetables"); GroundFrame.Core.Timetables.WTTTimeTableCollection TestTimeTableCollection = new GroundFrame.Core.Timetables.WTTTimeTableCollection(TestXML, new DateTime(2018, 7, 1)); List <MapperLocationNode> LocationMapperNodes = TestTimeTableCollection.GetMapperLocationNodes().OrderBy(x => x.SimSigCode).ToList(); Assert.Equal(24, LocationMapperNodes.Count); }
/// <summary> /// Populates the object from the supplied JSON /// </summary> /// <param name="JSON">The JSON string representing the WTTHeader object</param> private void PopulateFromJSON(string JSON) { //JSON argument will already have been validated in the constructor try { WTTTimeTableCollection Temp = JsonConvert.DeserializeObject <WTTTimeTableCollection>(JSON, new WTTTimeTableCollectionConverter()); this._StartDate = Temp.StartDate; this._TimeTables = Temp.ToList(); } catch (Exception Ex) { throw new ApplicationException(ExceptionHelper.GetStaticException("ParseWTTTimeTableCollectionJSONError", null, Globals.UserSettings.GetCultureInfo()), Ex); } }
public void WTTTimeTableCollection_Method_ToJSON() { string FullPath = new Uri($@"{AppDomain.CurrentDomain.BaseDirectory}Resources\TestWTT_4.8.xml").LocalPath; XElement TestXML = XDocument.Load(FullPath).Element("SimSigTimetable").Element("Timetables"); GroundFrame.Core.Timetables.WTTTimeTableCollection TestTimeTableCollection = new GroundFrame.Core.Timetables.WTTTimeTableCollection(TestXML, new DateTime(2018, 7, 1)); Assert.Equal(205, TestTimeTableCollection.Count); //Convert header to JSON string JSONTimeTableCollection = TestTimeTableCollection.ToJSON(); //Deserialize the JSON string back to an WTTHeader object GroundFrame.Core.Timetables.WTTTimeTableCollection JSONWTTTTimeTableCollection = new Timetables.WTTTimeTableCollection(JSONTimeTableCollection); Assert.Equal(205, JSONWTTTTimeTableCollection.Count); //Check both WTTHeader objects are equal Assert.Equal(TestTimeTableCollection.ToString(), JSONWTTTTimeTableCollection.ToString()); Assert.Equal(TestTimeTableCollection.StartDate, JSONWTTTTimeTableCollection.StartDate); Assert.Equal(TestTimeTableCollection.Count, JSONWTTTTimeTableCollection.Count); }
public void WTTTimeTableCollection_Method_GetByHeadCode() { string FullPath = new Uri($@"{AppDomain.CurrentDomain.BaseDirectory}Resources\TestWTT_4.8.xml").LocalPath; XElement XMLTestTimeTables = XDocument.Load(FullPath).Element("SimSigTimetable").Element("Timetables"); XElement XMLTestTimeTable = XMLTestTimeTables.Elements("Timetable").Where(x => x.Element("ID").Value.ToString() == "1R09").FirstOrDefault(); GroundFrame.Core.Timetables.WTTTimeTableCollection TestTimeTableCollection = new GroundFrame.Core.Timetables.WTTTimeTableCollection(XMLTestTimeTables, new DateTime(2018, 7, 1)); GroundFrame.Core.Timetables.WTTTimeTable TestTimeTable = TestTimeTableCollection.GetByHeadCode("1R09").First(); Assert.Equal(XMLTestTimeTable.Element("ID").Value.ToString(), TestTimeTable.Headcode); Assert.Equal((WTTAccelBrakeIndex)Convert.ToInt32(XMLTestTimeTable.Element("AccelBrakeIndex").Value), TestTimeTable.AccelBrakeIndex); Assert.Equal(Convert.ToInt32(XMLTestTimeTable.Element("AsRequiredPercent").Value.ToString()), TestTimeTable.RunAsRequiredPercentage); if (XMLTestTimeTable.Element("DepartTime") != null) { Assert.Equal(new GroundFrame.Core.Timetables.WTTTime(Convert.ToInt32(XMLTestTimeTable.Element("DepartTime").Value.ToString())).Seconds, TestTimeTable.DepartTime.Seconds); } else { Assert.Null(TestTimeTable.DepartTime); } Assert.Equal(XMLTestTimeTable.Element("Description").Value.ToString(), TestTimeTable.Description); Assert.Equal(new GroundFrame.Core.Length(Convert.ToInt32(XMLTestTimeTable.Element("SeedingGap").Value.ToString())).Meters, TestTimeTable.SeedingGap.Meters); if (XMLTestTimeTable.Element("EntryPoint") != null) { Assert.Equal(XMLTestTimeTable.Element("EntryPoint").Value.ToString(), TestTimeTable.EntryPoint); } else { Assert.Null(TestTimeTable.EntryPoint); } Assert.Equal(new GroundFrame.Core.Timetables.WTTSpeed(Convert.ToInt32(XMLTestTimeTable.Element("MaxSpeed").Value.ToString())).MPH, TestTimeTable.MaxSpeed.MPH); Assert.Equal(new GroundFrame.Core.Timetables.WTTSpeedClass(Convert.ToInt32(XMLTestTimeTable.Element("SpeedClass").Value.ToString())).Bitwise, TestTimeTable.SpeedClass.Bitwise); Assert.Equal(new GroundFrame.Core.Length(Convert.ToInt32(XMLTestTimeTable.Element("TrainLength").Value.ToString())).Meters, TestTimeTable.TrainLength.Meters); Assert.Equal(new GroundFrame.Core.Electrification(XMLTestTimeTable.Element("Electrification").Value.ToString()).ToString(), TestTimeTable.Electrification.ToString()); Assert.Equal(new GroundFrame.Core.Electrification(XMLTestTimeTable.Element("StartTraction").Value.ToString()).ToString(), TestTimeTable.StartTraction.ToString()); Assert.Equal(XMLTestTimeTable.Element("Category").Value.ToString(), TestTimeTable.SimSigTrainCategoryID); Assert.Equal(XMLTestTimeTable.Element("Trips").Elements("Trip").Count(), TestTimeTable.Trip.Count()); }