Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to nested serialization #65

Merged
merged 8 commits into from
Aug 5, 2024
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
4 changes: 2 additions & 2 deletions CSVFile.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
<description>Tiny and fast CSV and TSV parsing library (40KB) with zero dependencies. Compatible with DotNetFramework (2.0 onwards) and DotNetCore.</description>
<icon>docs/icons8-spreadsheet-96.png</icon>
<releaseNotes>
July 18, 2023
August 5, 2024

* Add serialization options for arrays
* Add serialization options for arrays and objects
</releaseNotes>
<readme>docs/README.md</readme>
<copyright>Copyright 2006 - 2024</copyright>
Expand Down
1 change: 1 addition & 0 deletions csharp-csv-reader.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{E92F982D
icons8-spreadsheet-96.png = icons8-spreadsheet-96.png
LICENSE = LICENSE
README.md = README.md
.github\workflows\nuget-publish.yml = .github\workflows\nuget-publish.yml
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "src.net50", "src\net50\src.net50.csproj", "{C78A66F7-113D-452A-989B-306CD6534E7B}"
Expand Down
126 changes: 76 additions & 50 deletions src/CSV.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
/// <summary>
/// Root class that contains static functions for straightforward CSV parsing
/// </summary>
public static class CSV

Check warning on line 29 in src/CSV.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu

Rename class 'CSV' to match pascal case naming rules, consider using 'Csv'.
{
/// <summary>
/// Use this to determine what version of DotNet was used to build this library
Expand Down Expand Up @@ -206,7 +206,7 @@
/// <param name="list">The array of objects to serialize</param>
/// <param name="settings">The CSV settings to use when exporting this array (Default: CSV)</param>
/// <returns>The completed CSV string representing one line per element in list</returns>
public static string Serialize<T>(IEnumerable<T> list, CSVSettings settings = null) where T : class, new()

Check warning on line 209 in src/CSV.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu

All 'Serialize' method overloads should be adjacent.
{
if (settings == null)
{
Expand Down Expand Up @@ -358,68 +358,94 @@

// Special cases for other types of serialization
string s;
var itemType = item.GetType();
var interfaces = itemType.GetInterfaces();
bool isEnumerable = false;
if (itemType != typeof(string))
if (item is string)
{
foreach (var itemInterface in interfaces)
{
if (itemInterface == typeof(IEnumerable))
{
isEnumerable = true;
}
}
}

if (item is DateTime)
s = item as string;
}
else if (item is DateTime)
{
s = ((DateTime)item).ToString(settings.DateTimeFormat);
}
else if (isEnumerable)
else
{
IEnumerable enumerable = item as IEnumerable;
s = string.Empty;
switch (settings.NestedArrayBehavior)
var itemType = item.GetType();
var interfaces = itemType.GetInterfaces();
bool isEnumerable = false;
if (itemType != typeof(string))
{
case ArrayOptions.ToString:
s = item.ToString();
break;
case ArrayOptions.CountItems:
// from https://stackoverflow.com/questions/3546051/how-to-invoke-system-linq-enumerable-count-on-ienumerablet-using-reflection
if (enumerable != null)
foreach (var itemInterface in interfaces)
{
if (itemInterface == typeof(IEnumerable))
{
int enumerableCount = 0;
var iter = enumerable.GetEnumerator();
using (iter as IDisposable)
isEnumerable = true;
}
}
}

// Treat enumerables as a simple class of objects that can be unrolled
if (isEnumerable)
{
IEnumerable enumerable = item as IEnumerable;
s = string.Empty;
switch (settings.NestedArrayBehavior)
{
case ArrayOptions.ToString:
s = item.ToString();
break;
case ArrayOptions.CountItems:
if (enumerable != null)
{
while (iter.MoveNext())
int enumerableCount = 0;
var iter = enumerable.GetEnumerator();
using (iter as IDisposable)
{
enumerableCount++;
while (iter.MoveNext())
{
enumerableCount++;
}
}
s = enumerableCount.ToString();
}

s = enumerableCount.ToString();
}

break;
case ArrayOptions.TreatAsNull:
if (settings.AllowNull)
{
s = settings.NullToken;
}
break;
case ArrayOptions.RecursiveSerialization:
if (enumerable != null)
{
s = ItemsToCsv(enumerable, settings, riskyChars, forceQualifierTypes);
}
break;
break;
case ArrayOptions.TreatAsNull:
if (settings.AllowNull)
{
s = settings.NullToken;
}
else
{
s = string.Empty;
}
break;
case ArrayOptions.RecursiveSerialization:
if (enumerable != null)
{
s = ItemsToCsv(enumerable, settings, riskyChars, forceQualifierTypes);
}
else
{
s = string.Empty;
}
break;
}
}
else if (itemType.IsClass && settings.NestedObjectBehavior == ObjectOptions.RecursiveSerialization)
{
var nestedItems = new List<object>();
foreach (var field in itemType.GetFields())
{
nestedItems.Add(field.GetValue(item));
}
foreach (var prop in itemType.GetProperties())
{
nestedItems.Add(prop.GetValue(item, null));
}
s = ItemsToCsv(nestedItems, settings, riskyChars, forceQualifierTypes);
}
else
{
s = item.ToString();
}
}
else
{
s = item.ToString();
}

// Check if this item requires qualifiers
Expand Down
27 changes: 24 additions & 3 deletions src/CSVSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public enum ArrayOptions
{
/// <summary>
/// Use built-in string conversion, which renders arrays as `MyObject[]`
/// Use built-in string conversion, which renders arrays as `MyNamespace.MyObject[]`
/// </summary>
ToString,

Expand All @@ -29,6 +29,22 @@
/// </summary>
CountItems,

/// <summary>
/// Serialize child arrays recursively using the same settings
/// </summary>
RecursiveSerialization,
}

/// <summary>
/// Defines the behavior of CSV Serialization when a nested object (class) is encountered
/// </summary>
public enum ObjectOptions
{
/// <summary>
/// Use built-in string conversion, which renders as `MyNamespace.MyObject`
/// </summary>
ToString,

/// <summary>
/// Serialize child objects recursively using the same settings
/// </summary>
Expand All @@ -38,7 +54,7 @@
/// <summary>
/// Settings to configure how a CSV file is parsed
/// </summary>
public class CSVSettings

Check warning on line 57 in src/CSVSettings.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu

Rename class 'CSVSettings' to match pascal case naming rules, consider using 'CsvSettings'.
{
/// <summary>
/// The character used to delimit individual fields in the CSV.
Expand Down Expand Up @@ -160,9 +176,14 @@
public string DateTimeFormat { get; set; } = "o";

/// <summary>
/// The behavior to use when serializing a column of an array type
/// The behavior to use when serializing a column that is an array or enumerable type
/// </summary>
public ArrayOptions NestedArrayBehavior { get; set; } = ArrayOptions.ToString;

/// <summary>
/// The behavior to use when serializing a column that is a class
/// </summary>
public ArrayOptions NestedArrayBehavior = ArrayOptions.TreatAsNull;
public ObjectOptions NestedObjectBehavior { get; set; } = ObjectOptions.ToString;

/// <summary>
/// Standard comma-separated value (CSV) file settings
Expand Down
56 changes: 52 additions & 4 deletions tests/SerializationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public class TestClassThree
public List<Guid> NullableList { get; set; }
}

public class TestClassFour
{
public string Name { get; set; }
public TestClassTwo Details { get; set; }
}

[Test]
public void TestObjectSerialization()
{
Expand Down Expand Up @@ -136,10 +142,7 @@ public void TestNullSerialization()
}

/// <summary>
/// Arrays and child objects aren't well suited for complex serialization within a CSV file.
/// However, we have options:
/// * ToString just converts it to "MyClass[]"
/// * CountItems just produces the number of elements in the array
/// Tests that validate whether we can serialize arrays within arrays
/// </summary>
[Test]
public void TestArraySerialization()
Expand Down Expand Up @@ -186,6 +189,51 @@ public void TestArraySerialization()
+ $"Test,\"a,b,c\",\"1,2,3\",\"True,False,True,False\",,NULL{Environment.NewLine}", recursiveCsv);
}

/// <summary>
/// Tests that validate whether we can serialize objects within arrays
/// </summary>
[Test]
public void TestNestedObjectSerialization()
{
var list = new List<TestClassFour>();
list.Add(new TestClassFour()
{
Name = "Non-Null Test",
Details = new TestClassTwo()
{
FirstColumn = "Hello World!",
SecondColumn = 42,
ThirdColumn = EnumTestType.Third,
}
});
list.Add(new TestClassFour()
{
Name = "Null Test",
Details = null
});

// Serialize to a CSV string using ToString
// This was the default behavior in CSVFile 3.1.2 and earlier - it's pretty ugly!
var options = new CSVSettings()
{
HeaderRowIncluded = true,
NestedObjectBehavior = ObjectOptions.ToString,
NullToken = "NULL",
AllowNull = true,
};
var toStringCsv = CSV.Serialize(list, options);
Assert.AreEqual($"Name,Details{Environment.NewLine}"
+ $"Non-Null Test,CSVTestSuite.SerializationTest+TestClassTwo{Environment.NewLine}"
+ $"Null Test,NULL{Environment.NewLine}", toStringCsv);

// Serialize to a CSV string using counts
options.NestedObjectBehavior = ObjectOptions.RecursiveSerialization;
var recursiveCsv = CSV.Serialize(list, options);
Assert.AreEqual($"Name,Details{Environment.NewLine}"
+ $"Non-Null Test,\"Hello World!,42,Third\"{Environment.NewLine}"
+ $"Null Test,NULL{Environment.NewLine}", recursiveCsv);
}

[Test]
public void TestCaseInsensitiveDeserializer()
{
Expand Down
Loading