diff --git a/SaneTsv/SaneTsv.cs b/SaneTsv/SaneTsv.cs index ff3b83f..949e59d 100644 --- a/SaneTsv/SaneTsv.cs +++ b/SaneTsv/SaneTsv.cs @@ -1174,13 +1174,23 @@ public class SaneTsv public static byte[] SerializeCommentedTsv(IList data, string fileComment) where T : CommentedTsvRecord { - return SerializeTsv(data, FormatType.COMMENTED_TSV); + return SerializeTsv(data, FormatType.COMMENTED_TSV, fileComment); } - protected static byte[] SerializeTsv(IList data, FormatType tsvFormat) + protected static byte[] SerializeTsv(IList data, FormatType tsvFormat, string fileComment = null) { var bytes = new List(); + if (fileComment != null) + { + if (tsvFormat != FormatType.COMMENTED_TSV) + { + throw new Exception($"File comments are not valid for {tsvFormat}"); + } + + bytes.AddRange(Encoding.UTF8.GetBytes("#" + fileComment.Replace("\n", "\n#") + "\n")); + } + var columnTypes = new List(); var columnNames = new List(); var columnPropertyInfos = new List(); diff --git a/SaneTsv/SaneTsvTest/Program.cs b/SaneTsv/SaneTsvTest/Program.cs index 98ac12b..d736b68 100644 --- a/SaneTsv/SaneTsvTest/Program.cs +++ b/SaneTsv/SaneTsvTest/Program.cs @@ -759,6 +759,30 @@ internal class Program : SaneTsv } } + { + string testName = "File comment serde"; + + string testString1 = "#this is a file comment" + + "\n# and one more line since you're such a good customer" + + "\ncolumn1:type:boolean\tcolumn2:binary\tcolumnthree\\nyep:string" + + "\nTRUE\tvalue\\\\t\0woo\tvaluetrhee" + + "\nFALSE\tnother\tno\\ther"; + + + CommentedTsv parsed = SaneTsv.ParseCommentedTsv(Encoding.UTF8.GetBytes(testString1)); + + string reserialized = Encoding.UTF8.GetString(SaneTsv.SerializeCommentedTsv(parsed.Records, parsed.FileComment)); + + if (reserialized == testString1) + { + Console.WriteLine($"Passed {testName}"); + } + else + { + Console.WriteLine($"Failed {testName}"); + } + } + Console.WriteLine("Done with tests"); } }