-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCsvSerializer.cs
More file actions
144 lines (130 loc) · 5.54 KB
/
CsvSerializer.cs
File metadata and controls
144 lines (130 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Remoting.Messaging;
using System.Text;
using DEXS.IO.CSV.Attributes;
using LumenWorks.Framework.IO.Csv;
using DEXS.IO.CSV.Extensions;
namespace DEXS.IO.CSV
{
public class CsvSerializer<T> where T : class, new()
{
private readonly IEnumerable<CsvProperty> _csvProperties;
public CsvFormatOptions FormatOptions { get; set; }
public CsvSerializer()
{
var type = typeof(T);
FormatOptions = type.GetCustomAttribute<CsvFormatOptions>() ?? new CsvFormatOptions();
_csvProperties = CsvConvert.GetCSVProperties(type);
}
public void Serialize(Stream stream, IEnumerable<T> data, CsvFormatOptions formatOptions = null)
{
var csvFormatOptions = formatOptions ?? FormatOptions;
var sb = new StringBuilder();
sb.Append(GetHeader(csvFormatOptions));
sb.Append(FormatOptions.LineSeparator);
foreach (var items in data.Select(item => GetValueItems(item, csvFormatOptions)))
{
sb.Append(string.Join(csvFormatOptions.Separator.ToString(), items));
sb.Append(csvFormatOptions.LineSeparator);
}
using (var sw = new StreamWriter(stream))
{
sw.Write(sb.ToString().Trim());
}
}
public object ConvertTo(object value, Type t, string format = null)
{
try
{
if (t == typeof (DateTime)) return (value as string).ToDateTime(format);
if (!t.IsGenericType || t.GetGenericTypeDefinition() != typeof (Nullable<>))
try
{
return Convert.ChangeType(value, t);
}
catch
{
return Activator.CreateInstance(t);
}
var nc = new NullableConverter(t);
return nc.ConvertFrom(value);
}
catch (Exception ex)
{
throw new Exception($"Error occured trying to convert {value} as {t} using format {format}", ex);
}
}
public IList<T> Deserialize(StreamReader stream, CsvFormatOptions formatOptions = null)
{
var csvFormatOptions = formatOptions ?? FormatOptions;
using (var csv = new CachedCsvReader(stream, true, csvFormatOptions.Separator, csvFormatOptions.QuoteChar, csvFormatOptions.EscapeChar, '#', ValueTrimmingOptions.UnquotedOnly))
{
var items = new List<T>();
csv.MoveToStart();
while (csv.ReadNextRecord())
{
var item = new T();
_csvProperties.ToList().ForEach(p =>
{
// if (!string.IsNullOrEmpty(p.Attributes.Format)) return;
var csvValue = csv[p.Attributes.Name ?? p.Info.Name];
var val = ConvertTo(csvValue, p.Info.PropertyType);
p.Info.SetValue(item, val);
});
items.Add(item);
}
return items;
}
}
public string GetString(object o, string format = null)
{
if (o == null) return null;
var oType = o.GetType();
if (oType != typeof (DateTime)) return o.ToString();
// Else convert DateTime
const string defaultDateTime = "yyyy-MM-ddTHH:mm:ss.ffffffZ";
return ((DateTime)o).ToString(format ?? defaultDateTime);
}
public IEnumerable<string> GetValueItems(T item, CsvFormatOptions formatOptions = null)
{
var csvFormatOptions = formatOptions ?? FormatOptions;
var rawItems = _csvProperties.Select(cp => GetString(cp.Info.GetValue(item), cp.Attributes.Format));
return FormatValues(rawItems, csvFormatOptions);
}
public string GetHeader(CsvFormatOptions formatOptions = null)
{
var csvFormatOptions = formatOptions ?? FormatOptions;
var formattedHeaderItems = FormatValues(GetHeaderItems(), formatOptions);
return string.Join(csvFormatOptions.Separator.ToString(), formattedHeaderItems);
}
public IEnumerable<string> GetHeaderItems()
{
return _csvProperties.Select(cp => cp.Attributes.Name ?? cp.Info.Name);
}
public IEnumerable<string> FormatValues(IEnumerable<string> values, CsvFormatOptions options = null)
{
var ra = options ?? FormatOptions;
var formatedValues = values.Select(h =>
{
var result = h?.Replace(ra.QuoteChar.ToString(), $"{ra.QuoteEscape}{ra.QuoteChar}") ?? "";
ra.MustEscape.ToList().ForEach(c =>
{
result = result.Replace(c.ToString(), $"{ra.EscapeChar}{c}");
});
var quoteChar = (ra.ForceQuotes ||
result.Contains(ra.QuoteChar) ||
result.Contains(ra.Separator) ||
ra.MustQuote.Any(c => result.Contains(c)))
? ra.QuoteChar.ToString() : string.Empty;
return $"{quoteChar}{result}{quoteChar}";
});
return formatedValues;
}
}
}