//*-----------------------------------------------------------------------* //*-----------------------------------------------------------------------* //* ReadJson * //*-----------------------------------------------------------------------* /// <summary> /// Read JSON content from the reader. /// </summary> /// <param name="reader"> /// JSON reader. /// </param> /// <param name="objectType"> /// Data type of the object being converted. /// </param> /// <param name="existingValue"> /// The value being converted. /// </param> /// <param name="serializer"> /// JSON serializer. /// </param> /// <returns> /// Converted object. /// </returns> public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { AttributeItem attribute = null; AttributeCollection attributes = null; JToken jToken = null; AttributeCatalog result = new AttributeCatalog(); try { jToken = JToken.Load(reader); } catch { } if (jToken != null) { foreach (JToken obj in jToken) { attributes = new AttributeCollection(); result.Add(attributes); if (obj.Count() > 0) { JProperty item = (JProperty)obj.First; while (item != null) { attribute = new AttributeItem(); attribute.Name = item.Name; attribute.Value = item.Value.ToString(); attributes.Add(attribute); item = (JProperty)item.Next; } } } } return(result); }
//*-----------------------------------------------------------------------* //*-----------------------------------------------------------------------* //* Parse * //*-----------------------------------------------------------------------* /// <summary> /// Parse the source content and return a populated object containing /// the deserialized information. /// </summary> /// <param name="name"> /// Name of the new component item. /// </param> /// <param name="lines"> /// Collection of string source lines representing the raw information to /// be parsed. /// </param> /// <param name="projectPath"> /// Current project path. /// </param> /// <returns> /// Newly created component item. /// </returns> public static ComponentItem Parse(string name, StringCollection lines, string projectPath) { AttributeItem attribute = null; //AttributeCollection attributes = null; int eCount = 0; int eIndex = 0; string element = ""; AttributeCatalog elements = null; AttributeCollection entry = null; string filename = ""; int iCount = 0; FileInfo ifile = null; int iIndex = 0; string insert = ""; MatchCollection matches = null; ComponentItem result = new ComponentItem(); string[] sArray = new string[0]; result.mName = name; eCount = lines.Count; for (eIndex = 0; eIndex < eCount; eIndex++) { // Get the current line. element = lines[eIndex]; matches = Regex.Matches(element, @"(?i:\{Include\((?<f>[^\)]+)\)\})"); if (matches.Count > 0) { // Inserts were found. In this version, that means no other entries will // be present on this line. foreach (Match match in matches) { filename = Tools.GetValue(match, "f"); if (Tools.IsRelative(filename)) { filename = Path.Combine(projectPath, filename); } ifile = new FileInfo(filename); if (ifile.Exists) { //attributes = new AttributeCollection(); //result.Attributes.Add(attributes); insert = File.ReadAllText(ifile.FullName); elements = JsonConvert.DeserializeObject <AttributeCatalog>(insert); iCount = elements.Count; for (iIndex = 0; iIndex < iCount; iIndex++) { result.Attributes.Add(elements[iIndex]); } } else { Console.WriteLine( string.Format( "Error could not read file: [{0}]...", ifile.FullName)); } } } else { // No inserts present on the line. element = element.Trim(); if (element.StartsWith("[")) { // Collection of entries. elements = JsonConvert. DeserializeObject <AttributeCatalog>(element); iCount = elements.Count; for (iIndex = 0; iIndex < iCount; iIndex++) { result.Attributes.Add(elements[iIndex]); } } else if (element.StartsWith("{")) { // One element object. entry = JsonConvert.DeserializeObject <AttributeCollection>(element); result.Attributes.Add(entry); } else if (element.IndexOf(":") > -1) { // Name / Value. entry = new AttributeCollection(); sArray = element.Split(new char[] { ':' }); if (sArray.Length > 1) { attribute = new AttributeItem(); entry.Add(attribute); attribute.Name = sArray[0].Replace("\"", "").Trim(); attribute.Value = sArray[1].Replace("\"", "").Trim(); } result.Attributes.Add(entry); } } } return(result); }