Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit ce2e4b3

Browse files
committed
Add support for serializing List of dynamic or objects to CSV
1 parent eeded57 commit ce2e4b3

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

src/ServiceStack.Text/CsvWriter.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,10 @@ public static void Write(TextWriter writer, IEnumerable<T> records)
229229
return;
230230
}
231231

232-
if (typeof(T) == typeof(Dictionary<string, object>))
232+
if (typeof(T).IsAssignableFromType(typeof(Dictionary<string, object>)))
233233
{
234-
CsvDictionaryWriter.Write(writer, (IEnumerable<Dictionary<string, object>>)records);
234+
var dynamicList = records.Select(x => x.ToObjectDictionary()).ToList();
235+
CsvDictionaryWriter.Write(writer, dynamicList);
235236
return;
236237
}
237238

src/ServiceStack.Text/ReflectionExtensions.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,25 @@ public static Type GetCollectionType(this Type type)
16851685
{
16861686
return type.ElementType() ?? type.GetTypeGenericArguments().FirstOrDefault();
16871687
}
1688+
1689+
public static Dictionary<string, object> ToObjectDictionary<T>(this T obj)
1690+
{
1691+
var dict = new Dictionary<string, object>();
1692+
1693+
foreach (var pi in obj.GetType().GetSerializableProperties())
1694+
{
1695+
dict[pi.Name] = pi.GetValue(obj, null);
1696+
}
1697+
1698+
if (JsConfig.IncludePublicFields)
1699+
{
1700+
foreach (var fi in obj.GetType().GetSerializableFields())
1701+
{
1702+
dict[fi.Name] = fi.GetValue(obj);
1703+
}
1704+
}
1705+
return dict;
1706+
}
16881707
}
16891708

16901709
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Collections.Generic;
2+
using System.Globalization;
3+
using System.IO;
4+
using System.Text;
5+
using NUnit.Framework;
6+
7+
namespace ServiceStack.Text.Tests
8+
{
9+
[TestFixture]
10+
public class CsvTypeTests
11+
{
12+
private int id = 0;
13+
static string[] Names = new[] { "Foo", "Bar" };
14+
15+
object Create(string name)
16+
{
17+
return new { id = ++id, name = name };
18+
}
19+
20+
[Test]
21+
public void Can_serialize_Dynamic_List()
22+
{
23+
List<dynamic> rows = Names.Map(Create);
24+
var csv = rows.ToCsv();
25+
Assert.That(csv, Is.EqualTo("id,name\r\n1,Foo\r\n2,Bar\r\n"));
26+
}
27+
28+
[Test]
29+
public void Can_serialize_Dynamic_Objects()
30+
{
31+
List<object> rows = Names.Map(Create);
32+
var csv = rows.ToCsv();
33+
Assert.That(csv, Is.EqualTo("id,name\r\n1,Foo\r\n2,Bar\r\n"));
34+
}
35+
}
36+
}

tests/ServiceStack.Text.Tests/ServiceStack.Text.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
</ItemGroup>
178178
<ItemGroup>
179179
<Compile Include="AttributeTests.cs" />
180+
<Compile Include="CsvTypeTests.cs" />
180181
<Compile Include="JsonTests\JsonEnumTests.cs" />
181182
<Compile Include="JsonTests\InvalidJsonTests.cs" />
182183
<Compile Include="JsonTests\TypeInfoTests.cs" />

0 commit comments

Comments
 (0)