diff --git a/src/MiniExcel.Csv/CsvConfiguration.cs b/src/MiniExcel.Csv/CsvConfiguration.cs index e6fdfb65..8f614bf8 100644 --- a/src/MiniExcel.Csv/CsvConfiguration.cs +++ b/src/MiniExcel.Csv/CsvConfiguration.cs @@ -11,7 +11,18 @@ public class CsvConfiguration : MiniExcelBaseConfiguration public char Seperator { get; set; } = ','; public string NewLine { get; set; } = "\r\n"; public bool ReadLineBreaksWithinQuotes { get; set; } = true; - public bool ReadEmptyStringAsNull { get; set; } = false; + + /// + /// Empty fields will be converted to null when queried as strings, or to the default value of the corresponding mapped type. + /// + public bool ReadEmptyFieldsAsDefault { get; set; } = false; + + [Obsolete("Please use the ReadEmptyFieldsAsDefault property instead")] + public bool ReadEmptyStringAsNull + { + get => ReadEmptyFieldsAsDefault; + set => ReadEmptyFieldsAsDefault = value; + } /// /// When set to true, rows with fewer columns than the header are padded with default values diff --git a/src/MiniExcel.Csv/CsvReader.cs b/src/MiniExcel.Csv/CsvReader.cs index 5bc62c6f..58dbb3db 100644 --- a/src/MiniExcel.Csv/CsvReader.cs +++ b/src/MiniExcel.Csv/CsvReader.cs @@ -108,7 +108,7 @@ internal CsvReader(Stream stream, IMiniExcelConfiguration? configuration, bool l // todo: can we find a way to remove the redundant cell conversions for CSV? var maxCol = (_config.FillMissingColumns ? headRows.Count : read.Length) - 1; var cell = ExpandoHelper.CreateEmptyByIndices(maxCol, 0); - if (_config.ReadEmptyStringAsNull) + if (_config.ReadEmptyFieldsAsDefault) { for (int i = 0; i <= read.Length - 1; i++) cell[CellReferenceConverter.GetAlphabeticalIndex(i)] = read[i] is var value and not "" ? value : null; diff --git a/tests/MiniExcel.Csv.Tests/Issues/GithubIssuesTests.cs b/tests/MiniExcel.Csv.Tests/Issues/GithubIssuesTests.cs index 482741dd..fa3f0744 100644 --- a/tests/MiniExcel.Csv.Tests/Issues/GithubIssuesTests.cs +++ b/tests/MiniExcel.Csv.Tests/Issues/GithubIssuesTests.cs @@ -695,7 +695,7 @@ public void Issue979NoHeader() var csvConfig = new CsvConfiguration { AlwaysQuote = true, - ReadEmptyStringAsNull = true + ReadEmptyFieldsAsDefault = true }; var data = """ diff --git a/tests/MiniExcel.Csv.Tests/Main/MiniExcelCsvAsyncTests.cs b/tests/MiniExcel.Csv.Tests/Main/MiniExcelCsvAsyncTests.cs index ed2b4129..4fd7201e 100644 --- a/tests/MiniExcel.Csv.Tests/Main/MiniExcelCsvAsyncTests.cs +++ b/tests/MiniExcel.Csv.Tests/Main/MiniExcelCsvAsyncTests.cs @@ -328,7 +328,7 @@ await _csvExporter.ExportAsync(path, new[] Assert.Equal(string.Empty, rows[1].C2); } - var config = new CsvConfiguration { ReadEmptyStringAsNull = true }; + var config = new CsvConfiguration { ReadEmptyFieldsAsDefault = true }; await using (var stream = File.OpenRead(path)) { var rows = _csvImporter.Query(stream, configuration: config).ToList();