De-Serializing CSV File

We have a business need to deserialize a csv file row by row, to a defined class, using the row headers in the csv as the mapping. We had a few questions about if we can use FromCSV in ServiceStack.Text to serve our needs:

  1. If a field value cannot be serialized to the correct type (e.g. they submit 42 in a DateTime field) we need to be able to let them know the specific field which caused the error. I couldn’t find any documentation on exception/error handling during the deserialization. Is this possible?
  2. Some of the types in the class are Enums - will the deserializer fail if the value provided doesn’t fall within the enum values?

If FromCSV doesn’t support these things, then is there a way to accomplish them with the ServcieStack.Text libraries or am I better off using something like FileHelpers?

thanks!

No ServiceStacks CSV serialiser does not support these exact features. If the data doesn’t match the Type it’s deserializing into the change the property Type to a string so you can then manually copy it into a different Type with stricter properties which will then let you know the exact row and property that throws an Exception when you try to convert it.

OK - thanks. Seems like I’d be re-writing the whole conversion at that point. FileHelpers it is :smile:

Just to throw in, https://joshclose.github.io/CsvHelper/ is a great csv deserialization library. It works identically to your requirements. I’m not associated with them, just have used it in several projects.

2 Likes