public static List <List <string> > ParseString(string csvString, CsvParser.DelimiterType delimiterType = CsvParser.DelimiterType.Auto, bool ignoreEmptyLines = true) { List <List <string> > list = new List <List <string> >(); CsvParser.ParseString(csvString, new CsvParser.ParserCallback(list.Add), delimiterType, ignoreEmptyLines); return(list); }
public static string ConvertToCsv(List <List <string> > sourceData, CsvParser.DelimiterType delimiterType = CsvParser.DelimiterType.Comma) { StringBuilder stringBuilder = new StringBuilder(); char c; switch (delimiterType) { case CsvParser.DelimiterType.Comma: c = ','; break; case CsvParser.DelimiterType.Semicolon: c = ';'; break; case CsvParser.DelimiterType.Tab: c = '\t'; break; default: throw new ArgumentException("Auto or unknown type of delimiter can't be used for export", "delimiterType"); } char[] anyOf = new char[] { '\n', '"', c }; foreach (List <string> current in sourceData) { for (int i = 0; i < current.Count; i++) { string text = current[i]; if (!string.IsNullOrEmpty(text)) { int num = text.IndexOfAny(anyOf); if (num != -1) { string value = text.Replace("\"", "\"\""); stringBuilder.Append('"'); stringBuilder.Append(value); stringBuilder.Append('"'); } else { stringBuilder.Append(text); } } if (i != current.Count - 1) { stringBuilder.Append(c); } } stringBuilder.AppendLine(); } return(stringBuilder.ToString()); }
public static List <T> DeserializeData <T>(string csvString, CsvDataDescriptor descriptor = null, int headerRow = 0, CsvParser.DelimiterType delimiter = CsvParser.DelimiterType.Auto) where T : new() { List <List <string> > parsedCsv = CsvParser.ParseString(csvString, delimiter, true); return(CsvSerializer.DeserializeData <T>(parsedCsv, descriptor, headerRow)); }
public static string SerializeData(IEnumerable <object> data, CsvDataDescriptor descriptor = null, CsvParser.DelimiterType delimiter = CsvParser.DelimiterType.Comma) { List <List <string> > list = new List <List <string> >(); if (descriptor == null) { List <Type> list2 = new List <Type>(); foreach (object current in data) { Type type = current.GetType(); if (!list2.Contains(type)) { list2.Add(type); } } descriptor = CsvDataDescriptor.CreateDescriptor(list2, null); } list.Add(descriptor.CreateCsvHeader()); foreach (object current2 in data) { list.Add(CsvSerializer.Serialize(current2, descriptor)); } return(CsvParser.ConvertToCsv(list, delimiter)); }
public static List <object> DeserializeData(List <List <string> > parsedCsv, CsvDataDescriptor descriptor, int headerRow = 0, CsvParser.DelimiterType delimiter = CsvParser.DelimiterType.Auto, IEnumerable <Type> customTypes = null) { if (descriptor == null) { if (customTypes == null) { throw new NullReferenceException("descriptor for generic import can't be null, you must at least specify types!"); } descriptor = CsvDataDescriptor.CreateDescriptor(customTypes, parsedCsv[headerRow]); } if (descriptor.ItemTypeColumn == -1) { throw new ArgumentException("descriptor for generic import must have type column set!"); } List <object> list = new List <object>(); for (int i = headerRow + 1; i < parsedCsv.Count; i++) { List <string> list2 = parsedCsv[i]; if (list2.Count >= descriptor.ItemTypeColumn + 1) { string text = list2[descriptor.ItemTypeColumn]; if (!string.IsNullOrEmpty(text)) { Type itemType = descriptor.GetItemType(text); if (itemType == null) { Debug.LogWarning("Can't instantiate type " + text + " this type is not in the descriptor!"); } else { object obj = Activator.CreateInstance(itemType); CsvSerializer.Deserialize(obj, list2, descriptor); list.Add(obj); } } } } return(list); }
public static List <object> DeserializeData(string csvString, CsvDataDescriptor descriptor, int headerRow = 0, CsvParser.DelimiterType delimiter = CsvParser.DelimiterType.Auto, IEnumerable <Type> customTypes = null) { List <List <string> > list = CsvParser.ParseString(csvString, delimiter, true); if (headerRow >= list.Count) { throw new ArgumentException("Header row index bigger then data!", "headerRow"); } return(CsvSerializer.DeserializeData(list, descriptor, headerRow, delimiter, customTypes)); }
public static string SerializeData <T>(IEnumerable <T> data, CsvDataDescriptor descriptor = null, CsvParser.DelimiterType delimiter = CsvParser.DelimiterType.Comma) { List <List <string> > list = new List <List <string> >(); if (descriptor == null) { descriptor = CsvDataDescriptor.CreateDescriptor(typeof(T), null); } list.Add(descriptor.CreateCsvHeader()); foreach (T current in data) { list.Add(CsvSerializer.Serialize(current, descriptor)); } return(CsvParser.ConvertToCsv(list, delimiter)); }
public static void ParseString(string csvString, CsvParser.ParserCallback dataHandler, CsvParser.DelimiterType delimiterType = CsvParser.DelimiterType.Auto, bool ignoreEmptyLines = true) { csvString = csvString.Replace("\r", string.Empty); CsvParser.ParserCallback parserCallback = delegate(List <string> dataRow) { if (ignoreEmptyLines && dataRow.Count == 0) { return; } dataHandler(dataRow); }; char c; switch (delimiterType) { case CsvParser.DelimiterType.Auto: c = CsvParser.DetectDelimiter(csvString); break; case CsvParser.DelimiterType.Comma: c = ','; break; case CsvParser.DelimiterType.Semicolon: c = ';'; break; case CsvParser.DelimiterType.Tab: c = '\t'; break; default: c = ','; break; } List <string> list = new List <string>(); char[] array = new char[] { c, '\n' }; int i = 0; while (i < csvString.Length) { int num; string text; if (csvString[i] == '"' && CsvParser.GetEndQuoteIndex(csvString, i, array, out num)) { if (num == -1) { Debug.LogError("Possibly corrupted CSV file, correct quote missing!"); text = csvString.Substring(i + 1, num - i - 1); } else { text = csvString.Substring(i + 1, num - i - 1); i = num + 1; text = text.Replace("\r", string.Empty); text = text.Replace("\"\"", "\""); } } else { int num2 = csvString.IndexOfAny(array, i); if (num2 == -1) { text = csvString.Substring(i); i += text.Length; text = text.Replace("\r", string.Empty); } else if (num2 == i) { text = string.Empty; } else { text = csvString.Substring(i, num2 - i); i += text.Length; text = text.Replace("\r", string.Empty); } } list.Add(text); if (i == csvString.Length) { parserCallback(list); break; } if (csvString[i] == c) { i++; } else { if (csvString[i] != '\n') { Debug.LogError("This shouldn't happen!"); int num3 = i - 50; if (num3 < 0) { num3 = 0; } Debug.Log(csvString.Substring(num3, 100)); break; } parserCallback(list); list = new List <string>(); if (csvString[i] == '\r') { i++; } i++; } if (i == csvString.Length) { if (list.Count > 0) { parserCallback(list); } break; } } }
public static List <List <string> > ParseFile(string fileName, CsvParser.DelimiterType delimiterType = CsvParser.DelimiterType.Auto, bool ignoreEmptyLines = true) { string csvString = File.ReadAllText(fileName); return(CsvParser.ParseString(csvString, delimiterType, true)); }