public static JObject FindFields(FileWrapper fileWrapper) { Dictionary <string, HashSet <string> > fields = new Dictionary <string, HashSet <string> >(); using (StreamReader streamReader = new StreamReader(new FileStream(fileWrapper.CreateInfo().ToString(), FileMode.Open, FileAccess.Read), Encoding.UTF8)) { string[] fieldNames = streamReader.ReadLine().Split(","); string line; while ((line = streamReader.ReadLine()) != null) { JObject JsonElement = new JObject(); string[] fieldValues = line.Split(","); for (int i = 0; i < fieldValues.Length; i++) { string fieldName = fieldNames[i]; string fieldValue = fieldValues[i]; if ("".Equals(fieldValue)) { continue; } if (!fields.ContainsKey(fieldName)) { fields.Add(fieldName, new HashSet <string>()); } fields[fieldName].Add(fieldValue); } } } return(FindFields(fields)); }
public static JObject ProcessCSV(FileWrapper fileWrapper) { JObject specJson = FindFields(fileWrapper); FileInfo csvFile = fileWrapper.CreateInfo(); JObject tableJson = new JObject(); using (StreamReader streamReader = new StreamReader(csvFile.OpenRead(), Encoding.UTF8)) { string[] fieldNames = streamReader.ReadLine().Split(","); string line; while ((line = streamReader.ReadLine()) != null) { JObject JsonElement = new JObject(); string[] fieldValues = line.Split(","); for (int i = 0; i < fieldValues.Length; i++) { string fieldName = fieldNames[i]; string fieldValue = fieldValues[i]; if ("".Equals(fieldValue)) { continue; } string type = (string)((JObject)specJson[fieldName])["type"]; if ("int bool".Equals(type)) { if ("0".Equals(fieldValue)) { JsonElement[fieldName] = false; } else if ("1".Equals(fieldValue)) { JsonElement[fieldName] = true; } else { throw new ArgumentException("Failed on " + csvFile + ". Bad key: " + type + "."); } } else if ("string".Equals(type)) { JsonElement[fieldName] = fieldValue; } else if ("float".Equals(type)) { JsonElement[fieldName] = float.Parse(fieldValue); } else if ("double".Equals(type)) { JsonElement[fieldName] = double.Parse(fieldValue); } else if ("int".Equals(type)) { JsonElement[fieldName] = int.Parse(fieldValue); } else if ("bool".Equals(type)) { JsonElement[fieldName] = bool.Parse(fieldValue); } else { throw new ArgumentException("Failed on " + csvFile + ". Bad key: " + type + "."); } } if (fieldNames[0].Equals("id")) { if (JsonElement.ContainsKey(fieldValues[0])) { throw new ArgumentException("Failed on " + csvFile + ". Multiple values for " + fieldNames[0] + " are " + fieldValues[0]); } tableJson[fieldValues[0]] = JsonElement; } else { string primary_key = fieldValues[0]; string secondary_key = fieldValues[1]; JObject JsonDirectory = (JObject)tableJson[primary_key]; if (!tableJson.ContainsKey(primary_key)) { tableJson[primary_key] = new JObject(); JsonDirectory = (JObject)tableJson[primary_key]; } else if (JsonDirectory.ContainsKey(secondary_key)) { throw new ArgumentException("Failed on " + csvFile + ". Multiple values for " + primary_key + ", " + secondary_key + " are " + fieldNames[0]); } JsonDirectory[secondary_key] = JsonElement; } } } return(tableJson); }