public IEnumerable <ExpandoObject> Parse(Ssv format) { var tableNames = format.GetTablesNames().ToArray(); foreach (var line in format.GetTableData(0)) { IDictionary <string, object> obj = new ExpandoObject(); var columnNamesLine = format.GetTableColumnNamesLine(); var columnNames = columnNamesLine.Values.Select(c => c.Trim()).ToArray(); for (int i = 0; i < line.Values.Count; i++) { var v = line.Values[i].Trim(); if (v.StartsWith("\"") && v.EndsWith("\"")) { obj[columnNames[i]] = v; } else if (int.TryParse(v, out int result)) { obj[columnNames[i]] = result; } else if (double.TryParse(v, out double dresult)) { obj[columnNames[i]] = dresult; } obj[columnNames[i]] = v; } yield return((ExpandoObject)obj); } }
public Ssv Parse(IEnumerable <string> contentLines) { var ssv = new Ssv(); //var previousLine = new Ssv.Line() { LineType = Ssv.LineType.Empty }; Ssv.Line previousLine = null; foreach (var line in contentLines) { //empty if (line == string.Empty) { previousLine = ssv.InsertEmptyLine(); continue; } //table if (line.TrimStart().StartsWith(SsvNotation.TableStartMark)) { var name = line.Trim().Replace(SsvNotation.TableStartMark, string.Empty).Replace(SsvNotation.TableEndMak, string.Empty); previousLine = ssv.InsertTableNameLine(name); continue; } //header if (line.TrimStart().StartsWith(SsvNotation.HeaderStartMark)) { var columnNames = line.Trim().Replace(SsvNotation.HeaderStartMark, string.Empty).Replace(SsvNotation.HeaderEndMark, string.Empty); var columnNamesRow = ParseHeaderRowLine(columnNames, previousLine?.LineType); previousLine = ssv.Insert(columnNamesRow); continue; } //comment if (line.TrimStart().StartsWith(SsvNotation.LineExclusionMark)) { var coment = line.Trim().Replace(SsvNotation.LineExclusionMark, string.Empty); ssv.Insert(new Ssv.CommentLine(coment)); continue; } //data var dataRow = ParseDataLine(line); ssv.Insert(dataRow); } return(ssv); }
public string Format(Ssv ssv) { var sb = new StringBuilder(); foreach (var line in ssv.Lines) { switch (line.LineType) { case Ssv.LineType.Data: sb.AppendLine(line.ToString(SsvNotation.ValueDelimiter)); continue; case Ssv.LineType.Excluded: if ((line as Ssv.CommentLine)?.Content != null) { sb.Append(SsvNotation.LineExclusionMark) .AppendLine((line as Ssv.CommentLine).Content); } else { sb.AppendLine(); } continue; case Ssv.LineType.TableName: sb.Append(SsvNotation.TableStartMark) .Append(line.Name) .AppendLine(SsvNotation.TableEndMak); continue; case Ssv.LineType.ColumnsNames: case Ssv.LineType.Custom: case Ssv.LineType.DefaultValues: sb.Append(SsvNotation.HeaderStartMark) .Append(line.ToString(SsvNotation.ValueDelimiter)) .AppendLine(SsvNotation.HeaderEndMark); continue; default: throw new ArgumentOutOfRangeException(); } } sb.Remove(sb.Length - SsvNotation.LineDelimiter.Length, SsvNotation.LineDelimiter.Length); return(sb.ToString()); }