CsvHelper从String解析boolean

时间:2017-10-27 08:29:45

标签: c# csv csvhelper

我目前正在尝试从一个布尔值的csv文件中解析一个值。我们注意到该值将成功解析YesY(无论如何),但不会解析NoN

我正在映射类图中的值,如下所示:

Map(m => m.Enabled).Name("Enabled").TypeConverterOption(false, string.Empty);

是否有理由将其读为Yes而不是No,是否有办法添加解析no的功能?

2 个答案:

答案 0 :(得分:1)

在4.0.3版本中,这可行。

void Main()
{
    using (var stream = new MemoryStream())
    using (var writer = new StreamWriter(stream))
    using (var reader = new StreamReader(stream))
    using (var csv = new CsvReader(reader))
    {
        writer.WriteLine("Id,Name,IsSomething");
        writer.WriteLine("1,one,Yes");
        writer.WriteLine("2,two,Y");
        writer.WriteLine("3,three,No");
        writer.WriteLine("4,four,N");
        writer.Flush();
        stream.Position = 0;

        csv.Configuration.RegisterClassMap<TestMap>();
        csv.GetRecords<Test>().ToList().Dump();
    }
}

public class Test
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsSomething { get; set; }
}

public sealed class TestMap : ClassMap<Test>
{
    public TestMap()
    {
        Map(m => m.Id);
        Map(m => m.Name);
        Map(m => m.IsSomething)
            .TypeConverterOption.BooleanValues(true, true, "Yes", "Y")
            .TypeConverterOption.BooleanValues(false, true, "No", "N");
    }
}

在2.16.3版本中,这有效。

void Main()
{
    using (var stream = new MemoryStream())
    using (var writer = new StreamWriter(stream))
    using (var reader = new StreamReader(stream))
    using (var csv = new CsvReader(reader))
    {
        writer.WriteLine("Id,Name,IsSomething");
        writer.WriteLine("1,one,Yes");
        writer.WriteLine("2,two,Y");
        writer.WriteLine("3,three,No");
        writer.WriteLine("4,four,N");
        writer.Flush();
        stream.Position = 0;

        csv.Configuration.RegisterClassMap<TestMap>();
        csv.GetRecords<Test>().ToList().Dump();
    }
}

public class Test
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsSomething { get; set; }
}

public sealed class TestMap : CsvClassMap<Test>
{
    public TestMap()
    {
        Map(m => m.Id);
        Map(m => m.Name);
        Map(m => m.IsSomething)
            .TypeConverterOption(true, "Yes", "Y")
            .TypeConverterOption(false, "No", "N");
    }
}

答案 1 :(得分:0)

我们在版本7中使用以下内容

单个映射的用途:

Map(m => m.IsSomething).Name("IsSomething").TypeConverter<MyBooleanConverter>();

应用于CSV阅读器的用途

csv.Configuration.TypeConverterCache.AddConverter<bool>(new MyBooleanConverter());

代码:

public sealed class MyBooleanConverter : DefaultTypeConverter
{
    public override object ConvertFromString(string text, IReaderRow row, MemberMapData memberMapData)
    {
        if(TryParseConvertBoolean(text, out bool result))
        {
            return result;
        }

        return null;
    }

    public static bool TryParseConvertBoolean(string value, out bool result)
    {
        result = false;
        try
        {
            if (string.IsNullOrWhiteSpace(value))
                return false;
            else
            {
                var lower = value.ToLower();

                if (lower == "no" || lower == "n")
                {
                    result = false;
                    return true;
                }
                else if (lower == "yes" || lower == "y")
                {
                    result = true;
                    return true;
                }
            }
        }
        catch { }

        return false;
    }
}