Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/MiniExcel.Csv/CsvConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// Empty fields will be converted to null when queried as strings, or to the default value of the corresponding mapped type.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
/// </summary>
public bool ReadEmptyFieldsAsDefault { get; set; } = false;

[Obsolete("Please use the ReadEmptyFieldsAsDefault property instead")]
public bool ReadEmptyStringAsNull
{
get => ReadEmptyFieldsAsDefault;
set => ReadEmptyFieldsAsDefault = value;
}

/// <summary>
/// When set to true, rows with fewer columns than the header are padded with default values
Expand Down
2 changes: 1 addition & 1 deletion src/MiniExcel.Csv/CsvReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/MiniExcel.Csv.Tests/Issues/GithubIssuesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ public void Issue979NoHeader()
var csvConfig = new CsvConfiguration
{
AlwaysQuote = true,
ReadEmptyStringAsNull = true
ReadEmptyFieldsAsDefault = true
};

var data = """
Expand Down
2 changes: 1 addition & 1 deletion tests/MiniExcel.Csv.Tests/Main/MiniExcelCsvAsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestDto>(stream, configuration: config).ToList();
Expand Down
Loading