Skip to content

Commit cf52368

Browse files
committed
Fixed serializer problem with the last value of each row
1 parent 71d53a1 commit cf52368

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

CSVSerializer.NET.CS/Deserializer.cs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
62
using System.IO;
73

84
namespace CSVSerializer
95
{
106
public class Deserializer
117
{
12-
private List<String> Headers;
13-
private List<Row> Rows;
148
private StreamReader File;
159

1610
public Deserializer(String FilePath)
1711
{
18-
File = new StreamReader(FilePath);
19-
12+
File = new StreamReader(FilePath); //may throw an exception if the file is not found, to be handled by the final user
2013
}
2114
public Deserializer(byte[] buffer)
2215
{
@@ -32,43 +25,53 @@ public Document Deserialize()
3225
Row row = new Row();
3326
char[] line = File.ReadLine().ToCharArray();
3427
char[] buffer = new char[line.Length]; //temporary buffer for composing values
28+
int bufferIndex = 0;
3529
for (int i = 0; i < line.Length; i++)
3630
{
37-
int bufferIndex = 0;
3831
if (line[i] == '\"')
3932
{
40-
int closingQuoteIndex = IndexOf(line, '\"', i + 1);
33+
int closingQuoteIndex = IndexOf(line, '\"', i + 1); //copies all the stuff between the double quotes in the buffer
4134
for (int a = i + 1; a < closingQuoteIndex; a++)
4235
{
4336
buffer[bufferIndex] = line[a];
4437
++bufferIndex;
4538
}
46-
i = closingQuoteIndex;
39+
i = closingQuoteIndex; //the index is now the closing double quote, next loop it will be the ,
4740
}
48-
else if (line[i] == ',')
41+
else if (line[i] == ',') //when it sees a comma, it dumps the content of the buffer in the row
4942
{
50-
String tmp = "";
51-
foreach (char c in buffer)
52-
tmp += c;
53-
Value value = new Value(tmp);
54-
row.AddValue(value);
43+
DumpBuffer(row, buffer);
44+
buffer = new char[line.Length]; //clears the array after dumping values
45+
bufferIndex = 0;
5546
}
5647
else
5748
{
5849
buffer[bufferIndex] = line[i];
5950
++bufferIndex;
6051
}
6152
}
62-
if (index == 0)
53+
DumpBuffer(row, buffer); //the last column must be added
54+
if (index == 0) //the first row is the header
6355
doc.SetHeader(row);
6456
else
6557
doc.AddRow(row);
6658
++index;
6759
}
6860
return doc;
6961
}
62+
//for a better reusability I created this method to avoid redundancy
63+
private static void DumpBuffer(Row row, char[] buffer)
64+
{
65+
String tmp = "";
66+
foreach (char c in buffer)
67+
if (c != '\0')
68+
tmp += c;
69+
else break; //otherwise keeps looping through the empty slot of the buffer
70+
Value value = new Value(tmp);
71+
row.AddValue(value);
72+
}
7073

71-
//returns the index of the first occurrence of the spacified element
74+
//returns the index of the first occurrence of the specified element
7275
private int IndexOf(char[] array, char element, int startingIndex = 0)
7376
{
7477
for(int i = startingIndex; i < array.Length; i++)

0 commit comments

Comments
 (0)