You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -66,8 +66,8 @@ var parser = serviceProvider.GetService<IParser>();
66
66
#### <ins>Step 2<ins>. Define the `IFileLine` implementation to parse a file record into a strongly typed line class.
67
67
Consider the file below. To parse a row into a C# class, you need to implement `IFileLine` interface. By doing this you create a strongly typed line representation for each row in the file.
68
68
```
69
-
|Mr|Jack Marias|Male|London|
70
-
|Dr|Bony Stringer|Male|New Jersey|
69
+
|Mr|Jack Marias|Male|London, UK|
70
+
|Dr|Bony Stringer|Male|New Jersey, US|
71
71
|Mrs|Mary Ward|Female||
72
72
|Mr|Robert Webb|||
73
73
```
@@ -100,7 +100,7 @@ public class Employee : IFileLine
100
100
public string Name { get; set; }
101
101
[Column(2)]
102
102
public EnumGender Sex { get; set; }
103
-
[Column(3, "London")]
103
+
[Column(3, "London, UK")]
104
104
public string Location { get; set; }
105
105
106
106
// IFileLine Members
@@ -118,13 +118,16 @@ ii. By providing the list of delimiter separated string values to parse method.
118
118
```
119
119
var lines = new[]
120
120
{
121
-
"|Mr|Jack Marias|Male|London|",
122
-
"|Dr|Bony Stringer|Male|New Jersey|",
121
+
"|Mr|Jack Marias|Male|London, UK|",
122
+
"|Dr|Bony Stringer|Male|New Jersey, US|",
123
123
};
124
124
125
125
var records = new Parser('|').Parse<Employee>(lines);
126
126
```
127
127
#### <ins>Step 3<ins>. Advanced Parsing of data using nested types in the FileLine class.
128
+
129
+
**Case 1**: Write your own Custom Converter to parse data to a custom type.
130
+
128
131
You could implement advance parsing of data by implementing `TypeConverter` class. Suppose we have to change the Name string property in Employee class above to a `NameType` property shown below.
129
132
```
130
133
public class Employee : IFileLine
@@ -210,7 +213,52 @@ public class NameType
210
213
}
211
214
}
212
215
```
213
-
Now parsing the file should hydrate data correctly to the Employee FileLine class and its nested name type.
216
+
Now parsing the file should hydrate the name correctly to the Employee FileLine class with nested name type.
217
+
218
+
**Case 2**: Use `CustomeConverter<T>` included with Parsely, where `T` is the custom type.
219
+
220
+
Please see example below on how out of the box converter can be used in defining custom `LocationType` class to include in Employee line class.
221
+
```
222
+
public class Employee : IFileLine
223
+
{
224
+
[Column(0)]
225
+
public string Title { get; set; }
226
+
[Column(1)]
227
+
public NameType Name { get; set; }
228
+
[Column(2)]
229
+
public EnumGender Sex { get; set; }
230
+
[Column(3, "London, UK")]
231
+
public LocationType Location { get; set; }
232
+
233
+
// IFileLine Members
234
+
public int Index { get; set; }
235
+
public IList<string> Errors { get; set; }
236
+
}
237
+
```
238
+
You need to derive the custom `LocationType` type from `ICustomType` and implement the `Parse(string)` method.
239
+
You also need to decorate the custom type with ` [TypeConverter(typeof(CustomConverter<LocationType>))]` attribute.
0 commit comments