Skip to content

TypeConverter to a nullable #772

@InsidiousForce

Description

@InsidiousForce

I'm trying to convert a tri-state boolean to a bool? field.

Unfortunately, the csv data has data like true,false,false,null,false - that is they have the word null for null fields. It's beyond aggravating.

Anyway, string fields, I was able to put together a typeconverter:

        public class NullableCsvStringConverter : StringConverter
        {
            public NullableCsvStringConverter()
            {
            }

            public override object ConvertFromString(string text, IReaderRow row, MemberMapData memberMapData)
            {
                return base.ConvertFromString(text == "null" ? string.Empty : text, row, memberMapData);
            }

            public override string ConvertToString(object value, IWriterRow row, MemberMapData memberMapData)
            {
                var text = value as string;
                return base.ConvertToString(text == "null" ? string.Empty : text, row, memberMapData);
            }
        }

and then I use it like this:

                var nullableCsvStringConverter = new NullableCsvStringConverter();
                Map(m => m.Email).Name("email").TypeConverter(nullableCsvStringConverter);

I tried to do the same thing with BooleanConverter and remove the null and replace it with string.empty then pass it to the base class, but it throws errors in BooleanConverter when it's empty rather than mapping to the bool? in my data object.

Am I going about this the right way? Maybe there's another place I can hook in to rewrite all columns with null as string.empty regardless of type before they get processed by the regular converters that detect type?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions