Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CsvHelper set default custom TypeConverter

Tags:

c#

csvhelper

With CsvHelper, when I want a custom parser (for example, I want a MyBooleanConverter with the input string is "f" will be false, "t" will be "true"). But with every class I have to write mapper:

public sealed class MyClassMap : CsvClassMap<MyClass>
{
    public MyClassMap()
    {
        Map( m => m.Id ).Index( 0 ).TypeConverter<MyBooleanConverter>();
    }
}

Or

[CsvHelper.TypeConversion.TypeConverter( typeof( MyBooleanConverter) )]
public Boolean MyObjectProperty { get; set; }

How can I set MyBooleanConverter as default for every boolean field and every Class?

like image 492
yelliver Avatar asked Feb 09 '17 09:02

yelliver


2 Answers

The CsvHelper library exposes a static TypeConverterFactory. You can simply remove the default Boolean converter and add your custom converter to replace it.

TypeConverterFactory.RemoveConverter<bool>();
TypeConverterFactory.AddConverter<bool>(new MyBooleanConverter());
like image 163
Michael Richardson Avatar answered Sep 30 '22 20:09

Michael Richardson


Just adding my code snippet to the following post to help understand the Type Converters built into CsvHelper. I needed to handle dates in the following format "yyyyMMdd" and it seemed to do the trick writing back and forth to CSV without the .net DateTime Parse error: "String was not recognized as a valid DateTime."

using (TextWriter writer = new StreamWriter(csvLocaitonAndName))
        {
            var csvUpdate = new CsvWriter(writer);
            csvUpdate.Configuration.TypeConverterCache.AddConverter<DateTime?>(new DateConverter("yyyyMMdd"));
            csvUpdate.Configuration.HasHeaderRecord = false;
            csvUpdate.WriteRecords(list);
        }

public class DateConverter : ITypeConverter
    {
        private readonly string _dateFormat;

        public DateConverter(string dateFormat)
        {
            _dateFormat = dateFormat;
        }

        public object ConvertFromString(string text, IReaderRow row, MemberMapData memberMapData)
        {
            if (!string.IsNullOrEmpty(text))
            {
                DateTime dt;
                DateTime.TryParseExact(text, _dateFormat,
                                       CultureInfo.InvariantCulture,
                                       DateTimeStyles.None,
                                       out dt);
                if (IsValidSqlDateTime(dt))
                {
                    return dt;
                }

            }

            return null;
        }
        public string ConvertToString(object value, IWriterRow row, MemberMapData memberMapData)
        {
            return ObjectToDateString(value, _dateFormat);
        }

        public string ObjectToDateString(object o, string dateFormat)
        {
            if (o == null) return string.Empty;

            DateTime dt;
            if (DateTime.TryParse(o.ToString(), out dt))
                return dt.ToString(dateFormat);
            else
                return string.Empty; 
        }
        public bool IsValidSqlDateTime(DateTime? dateTime)
        {
            if (dateTime == null) return true;

            DateTime minValue = DateTime.Parse(System.Data.SqlTypes.SqlDateTime.MinValue.ToString());
            DateTime maxValue = DateTime.Parse(System.Data.SqlTypes.SqlDateTime.MaxValue.ToString());

            if (minValue > dateTime.Value || maxValue < dateTime.Value)
                return false;

            return true;
        }
like image 37
Draz Avatar answered Sep 30 '22 19:09

Draz