public void CsvDocumentTest3() { var persons = new List <Person> { new Person { Name = "Akari", Age = 20 }, new Person { Name = "Kyoko", Age = 21 } }; var format = new CsvFormat(';', '\"'); var document = new CsvDocument <Person>(persons, format); Assert.AreEqual(2, document.Count); Assert.IsFalse(document.IsEmpty); Assert.AreEqual(CsvHeader.FromValues(format, "Name", "Age"), document.Header); Assert.AreEqual(new CsvFormat(';', '\"'), document.Format); }
/// <summary> /// Converts the <paramref name="source"/> enuemrable to a Csv string. /// </summary> /// <typeparam name="T">The enumerable type.</typeparam> /// <param name="source">The source enumerable to join.</param> /// <param name="format">(optional) Csv format to use during join.</param> /// <param name="selector"> /// (optional) Selector to convert entries to string equivalents. /// Defaults to <see cref="System.Object.ToString"/> if none specified. /// </param> /// <returns>Csv string of the joined entries.</returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="source"/> is null.</exception> public static string ToCsv <T>(this IEnumerable <T> source, CsvFormat format = CsvFormat.Normal, Func <T, string> selector = null) { if (source == null) { throw new ArgumentNullException(nameof(source)); } selector = selector ?? ((item) => item?.ToString()); var entries = source.Select(selector) .Select(entry => { return(format == CsvFormat.Normal ? Regex.IsMatch(entry, "[,]") ? $"\"{entry}\"" : entry : format == CsvFormat.Strict ? Regex.IsMatch(entry, "[,]") ? throw new InvalidOperationException($"Strict mode does not support nested commas.\r\nEntry: {entry}") : entry : null); }); return(string.Join(",", entries)); }
public void ContainsText_WithInput_ReturnsExpectedValue(string input, bool expectedValue) { var result = CsvFormat.ContainsText(input); Assert.AreEqual(expectedValue, result); }
//After configure called public static void AfterInit() { var specifiedContentType = config.DefaultContentType; if (config.EnableFeatures != Feature.All) { if ((Feature.Xml & config.EnableFeatures) != Feature.Xml) { config.IgnoreFormatsInMetadata.Add("xml"); } if ((Feature.Json & config.EnableFeatures) != Feature.Json) { config.IgnoreFormatsInMetadata.Add("json"); } if ((Feature.Jsv & config.EnableFeatures) != Feature.Jsv) { config.IgnoreFormatsInMetadata.Add("jsv"); } if ((Feature.Csv & config.EnableFeatures) != Feature.Csv) { config.IgnoreFormatsInMetadata.Add("csv"); } if ((Feature.Html & config.EnableFeatures) != Feature.Html) { config.IgnoreFormatsInMetadata.Add("html"); } if ((Feature.Soap11 & config.EnableFeatures) != Feature.Soap11) { config.IgnoreFormatsInMetadata.Add("soap11"); } if ((Feature.Soap12 & config.EnableFeatures) != Feature.Soap12) { config.IgnoreFormatsInMetadata.Add("soap12"); } } if ((Feature.Html & config.EnableFeatures) == Feature.Html) { HtmlFormat.Register(AppHost); } if ((Feature.Csv & config.EnableFeatures) == Feature.Csv) { CsvFormat.Register(AppHost); } if ((Feature.Markdown & config.EnableFeatures) == Feature.Markdown) { MarkdownFormat.Instance.MarkdownBaseType = config.MarkdownBaseType; MarkdownFormat.Instance.MarkdownGlobalHelpers = config.MarkdownGlobalHelpers; MarkdownFormat.Instance.Register(AppHost); } if (!string.IsNullOrEmpty(specifiedContentType)) { config.DefaultContentType = specifiedContentType; } else if (string.IsNullOrEmpty(config.DefaultContentType)) { config.DefaultContentType = ContentType.Json; } config.ServiceManager.AfterInit(); }
private CsvWriter GetWriter(CsvFormat format) { return(new CsvWriter(underlyingWriter, format)); }
/// <summary> /// Parses the <paramref name="source"/> string into an enumerable of <see cref="System.String"/>. /// </summary> /// <param name="source">The source string to parse.</param> /// <param name="format">(optional) Csv format to use during parsing.</param> /// <param name="selector">(optional) Selector to alter the <see cref="System.String"/> entries.</param> /// <returns>The enumerable of <see cref="System.String"/> entries.</returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="source"/> is null.</exception> public static IEnumerable <string> ParseCsv(this string source, CsvFormat format = CsvFormat.Normal, Func <string, string> selector = null) { return(source.ParseCsv <string>(format, selector ?? (entry => entry))); }
public async Task SaveEdgesAsCsv <TFrom, TTo, TSource>( EdgeType <TFrom, TTo, TSource> edgeType, CsvFormat csvFormat) where TSource : class { try { var fromPropertyName = edgeType.From.GetPropertyName(); var toPropertyName = edgeType.To.GetPropertyName(); var sb = new StringBuilder(); sb.Append("~id,~from,~to,~label"); if (csvFormat == CsvFormat.AutoTrainer) { sb.Append(",~fromLabels,~toLabels"); } if (edgeType.Properties.Any()) { foreach (var property in edgeType.Properties) { var propertyName = property.GetPropertyName(); sb.Append($",{propertyName}"); if (csvFormat == CsvFormat.Neptune) { var propertyTypeName = GetNeptunePropertyTypeName(property.GetPropertyType()); sb.Append($":{propertyTypeName}"); } } } sb.AppendLine(); var records = await RecordsProvider.GetRecords(edgeType.Predicate); foreach (var record in records) { var fromValue = edgeType.SourceType.GetProperty(fromPropertyName).GetValue(record); var toValue = edgeType.SourceType.GetProperty(toPropertyName).GetValue(record); if (fromValue == null || toValue == null) { continue; } var id = $"{edgeType.FromType.Name}:{fromValue}:{edgeType.ToType.Name}:{toValue}".CreateMD5(); sb.Append($"{id},{fromValue},{toValue},{edgeType.Label}"); if (csvFormat == CsvFormat.AutoTrainer) { sb.Append(",{ edgeType.FromType.Name},{ edgeType.ToType.Name}"); } if (edgeType.Properties.Any()) { foreach (var property in edgeType.Properties) { var propertyName = property.GetPropertyName(); var value = Convert.ToString(edgeType.SourceType.GetProperty(propertyName).GetValue(record)); if (csvFormat == CsvFormat.Neptune) { value = GetNeptunePropertyValue(property.GetPropertyType(), value); } if (value != null && value.Contains(",")) { value = $"\"{value.Replace("\"", "\"\"")}\""; } sb.Append($",{value}"); } } sb.AppendLine(); } await FileStore.SaveFile(GraphId, "edges", $"{edgeType.Label}.csv", sb.ToString()); } catch (Exception e) { Console.WriteLine(e); throw; } }
public void ShouldEscapeComma() { var actual = CsvFormat.Escape("a,comma"); Assert.AreEqual("\"a,comma\"", actual); }
public void ShouldThrowIfMissingEndQuote() { AssertUtils.AssertThrows <ArgumentException>( () => CsvFormat.Parse("one,two,\"three").Count() ); }
/// <summary> /// Reads the next csv record using the specified <see cref="StreamReader"/>. /// </summary> /// <param name="reader">The reader.</param> /// <param name="format">The format.</param> /// <returns>A list with the fields of the record</returns> /// <exception cref="FastCSV.CsvFormatException">If a quote is not closed.</exception> public static List <string>?ReadRecord(StreamReader reader, CsvFormat format) { if (reader.EndOfStream) { return(default);
private CsvReader GetReader(CsvFormat format) { return(new CsvReader(underlyingReader, format)); }
public void EqualsTest() { var format = new CsvFormat(',', '\"', QuoteStyle.WhenNeeded); Assert.IsTrue(format == CsvFormat.Default); }