Default values for NullValues
Small suggestion to discuss - NullValues (for TypeConverterOptions) shouldnt be empty by default? In my opionion treating "null" text as null shouldnt be standard approach.
I understand where you're coming from. What do you think should be used instead? An empty field (no characters) is just an empty string and doesn't signify null.
In my opinion empty string should be also null. Empty string is "better" than null - parsed text should never return null by default. ps I know that it is not big issue - changing current approach can be done in few lines.
Since a CSV file is text, I think a non value should be an empty string. Null is the absence of a value. It's like if the field was missing completely. So if you were to have a header named field that was missing from the file, I believe that is when null should be used.
I think I'm ok with removing default values for null though. I can't really find any sort of standard on what that should be. People have wide varying ideas based on their industry. When writing you can only wring string.empty. There's no actual way to write out another value for null, unless you write a custom type converter.
The reason NULL or null is used in the first place is because databases will represent their null values that way. Looks like other industries will use NA, N/A, long dash, null character, etc. Choosing the database standard doesn't really make sense.
What do you think about using the ASCII NULL value of \x0?
I just committed this change. https://github.com/JoshClose/CsvHelper/commit/3a2daa1b22db5a85743db4d081a2008ea74fddab
@JoshClose I am still not clear on this: when is the default value used now? When the string is null or empty?
string values will never be null unless you specify a null value to match. CSV files are all strings so null doesn't really make sense. Other types when empty, like int will be default( int ), and if it's Nullabe<int> it will be null.
If you want to have some sort of value be null, you'll need to add them to the list of null values. In your case, you can simply add string.Empty as a null value, and you'll get the functionality you're looking for.
Does that make sense?
I understand what you mean, but that is only true when reading from CSV files.
When writing CSV files though, you start with C# strings, which can be null or empty. Is in both cases the default value written instead?
I have a custom mapping with default values set (among other things) and the written CSV file only contains empty strings on the properties, which are empty/null.
I will try adding string.Empty as a null value as you said. Thanks for getting back to me so quickly!
When writing, an empty string or a null will write out the same thing. It's no value. Default values don't come into play at all.
Hi Josh, Is there a way to print default values for nullable types say, 0 for int?, 0.0 for double in one go using csv configuration? Currently, I am using ConvertUsing on each field for this, but this becomes tedious when we have huge number of fields
How could I achieve the behaviour that was removed here? OK, I've found it:
var opts = new TypeConverterOptions();
opts.NullValues.Add("null");
opts.NullValues.Add("NULL");
csv.Context.TypeConverterOptionsCache.AddOptions(typeof(string), opts);