@@ -44,6 +44,16 @@ public class TestClassTwo
4444 public EnumTestType ? ThirdColumn ;
4545 }
4646
47+ public class TestClassThree
48+ {
49+ public string Name { get ; set ; }
50+ public string [ ] StringArray { get ; set ; }
51+ public List < int > IntList { get ; set ; }
52+ public IEnumerable < bool > BoolEnumerable { get ; set ; }
53+ public List < Guid > GuidList { get ; set ; }
54+ public List < Guid > NullableList { get ; set ; }
55+ }
56+
4757 [ Test ]
4858 public void TestObjectSerialization ( )
4959 {
@@ -125,6 +135,57 @@ public void TestNullSerialization()
125135 }
126136 }
127137
138+ /// <summary>
139+ /// Arrays and child objects aren't well suited for complex serialization within a CSV file.
140+ /// However, we have options:
141+ /// * ToString just converts it to "MyClass[]"
142+ /// * CountItems just produces the number of elements in the array
143+ /// </summary>
144+ [ Test ]
145+ public void TestArraySerialization ( )
146+ {
147+ var list = new List < TestClassThree > ( ) ;
148+ list . Add ( new TestClassThree ( )
149+ {
150+ Name = "Test" ,
151+ StringArray = new [ ] { "a" , "b" , "c" } ,
152+ IntList = new List < int > { 1 , 2 , 3 } ,
153+ BoolEnumerable = new [ ] { true , false , true , false } ,
154+ GuidList = new List < Guid > ( ) ,
155+ } ) ;
156+
157+ // Serialize to a CSV string using ToString
158+ // This was the default behavior in CSVFile 3.1.2 and earlier - it's pretty ugly!
159+ var options = new CSVSettings ( )
160+ {
161+ HeaderRowIncluded = true ,
162+ NestedArrayBehavior = ArrayOptions . ToString ,
163+ NullToken = "NULL" ,
164+ AllowNull = true ,
165+ } ;
166+ var toStringCsv = CSV . Serialize ( list , options ) ;
167+ Assert . AreEqual ( $ "Name,StringArray,IntList,BoolEnumerable,GuidList,NullableList{ Environment . NewLine } "
168+ + $ "Test,System.String[],System.Collections.Generic.List`1[System.Int32],System.Boolean[],System.Collections.Generic.List`1[System.Guid],NULL{ Environment . NewLine } ", toStringCsv ) ;
169+
170+ // Serialize to a CSV string using counts
171+ options . NestedArrayBehavior = ArrayOptions . CountItems ;
172+ var countItemsCsv = CSV . Serialize ( list , options ) ;
173+ Assert . AreEqual ( $ "Name,StringArray,IntList,BoolEnumerable,GuidList,NullableList{ Environment . NewLine } "
174+ + $ "Test,3,3,4,0,NULL{ Environment . NewLine } ", countItemsCsv ) ;
175+
176+ // Serialize to a CSV string using counts
177+ options . NestedArrayBehavior = ArrayOptions . TreatAsNull ;
178+ var ignoreArraysCsv = CSV . Serialize ( list , options ) ;
179+ Assert . AreEqual ( $ "Name,StringArray,IntList,BoolEnumerable,GuidList,NullableList{ Environment . NewLine } "
180+ + $ "Test,NULL,NULL,NULL,NULL,NULL{ Environment . NewLine } ", ignoreArraysCsv ) ;
181+
182+ // And now for the magic: Recursive serialization!
183+ options . NestedArrayBehavior = ArrayOptions . RecursiveSerialization ;
184+ var recursiveCsv = CSV . Serialize ( list , options ) ;
185+ Assert . AreEqual ( $ "Name,StringArray,IntList,BoolEnumerable,GuidList,NullableList{ Environment . NewLine } "
186+ + $ "Test,\" a,b,c\" ,\" 1,2,3\" ,\" True,False,True,False\" ,,NULL{ Environment . NewLine } ", recursiveCsv ) ;
187+ }
188+
128189 [ Test ]
129190 public void TestCaseInsensitiveDeserializer ( )
130191 {
0 commit comments