/// <summary> /// Reads JSON file, parsing valid data into ClockShapeList and logging errors. /// </summary> /// <param name="fileName"></param> /// <param name="clockShapeList"></param> /// <param name="shapePath"></param> public ClocksFile(string fileName, List <ClockShape> clockShapeList, string shapePath) { ShapePath = shapePath; ClockShapeList = clockShapeList; JsonReader.ReadFile(fileName, TryParse); // Filter out objects with essentials properties that are either incomplete or invalid. ClockShapeList = ClockShapeList .Where(r => !string.IsNullOrEmpty(r.Name) && r.ClockType != null) .ToList(); }
/// <summary> /// Parses next item from JSON data, populating a ClockShape and issuing error messages. /// </summary> /// <param name="item"></param> /// <returns></returns> protected virtual bool TryParse(JsonReader item) { var stringValue = ""; switch (item.Path) { case "": // Ignore these items. break; case "[].": // Create an object with properties which are initially incomplete, so omissions can be detected and the object rejected later. ClockShape = new ClockShape(stringValue, null); ClockShapeList.Add(ClockShape); break; case "[].Name": // Parse the property with default value as invalid, so errors can be detected and the object rejected later. stringValue = item.AsString(stringValue); var path = ShapePath + stringValue; ClockShape.Name = path; if (string.IsNullOrEmpty(stringValue)) { return(false); } if (File.Exists(path) == false) { Trace.TraceWarning($"Non-existent shape file {path} referenced in animated.clocks-or"); } break; case "[].ClockType": stringValue = item.AsString(stringValue).ToLower(); if (stringValue == "analog") { ClockShape.ClockType = ClockType.Analog; } else { Trace.TraceWarning($"ClockType \"{stringValue}\" found, but \"analog\" expected"); return(false); } break; default: Trace.TraceWarning($"Unexpected entry \"{item.Path}\" found"); return(false); } return(true); }