Skip to content

Commit e1e43f2

Browse files
committed
Added new parameter ForceQuotesAroundValues
1 parent 091f274 commit e1e43f2

File tree

5 files changed

+48
-28
lines changed

5 files changed

+48
-28
lines changed

Frends.Csv.Tests/Tests.cs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -215,39 +215,50 @@ public void TestWriteFromListTable()
215215
new List<object>() {100, "Dilantin", "Melanie", date}
216216
};
217217

218-
var result = Csv.Create(new CreateInput() { InputType = CreateInputType.List, Delimiter = ";", Data = data, Headers = headers}, new CreateOption() { CultureInfo = "fi-FI" });
219-
Assert.AreEqual(result.Csv,
218+
var result = Csv.Create(new CreateInput() { InputType = CreateInputType.List, Delimiter = ";", Data = data, Headers = headers}, new CreateOption() { CultureInfo = "fi-FI", ForceQuotesAroundValues = false });
219+
Assert.AreEqual(
220220
@"Dosage;Drug;Patient;Date
221221
25;Indocin;David;1.1.2000 0.00.00
222222
50;Enebrel;Sam;1.1.2000 0.00.00
223223
10;Hydralazine;Christoff;1.1.2000 0.00.00
224224
21;""Combiv;ent"";Janet;1.1.2000 0.00.00
225225
100;Dilantin;Melanie;1.1.2000 0.00.00
226-
");
226+
", result.Csv);
227227
}
228228

229229
[Test]
230230
public void TestWriteFromJson()
231231
{
232232
var json = @"[{""cool"":""nice"", ""what"": ""no""}, {""cool"":""not"", ""what"": ""yes""}, {""cool"":""maybe"", ""what"": ""never""}]";
233233
var result = Csv.Create(new CreateInput() { InputType = CreateInputType.Json, Delimiter = ";", Json = json}, new CreateOption());
234-
Assert.AreEqual(result.Csv,
234+
Assert.AreEqual(
235235
@"cool;what
236236
nice;no
237237
not;yes
238238
maybe;never
239-
");
239+
", result.Csv);
240240
}
241241

242242
[Test]
243243
public void TestNullInputValue()
244244
{
245245
var json = @"[{""ShouldStayNull"":""null"", ""ShouldBeReplaced"": null}]";
246246
var result = Csv.Create(new CreateInput() { InputType = CreateInputType.Json, Delimiter = ";", Json = json }, new CreateOption() { ReplaceNullsWith = "replacedvalue" });
247-
Assert.AreEqual(result.Csv,
247+
Assert.AreEqual(
248248
@"ShouldStayNull;ShouldBeReplaced
249249
null;replacedvalue
250-
");
250+
", result.Csv);
251+
}
252+
253+
[Test]
254+
public void TestInputValueWithForceQuotes()
255+
{
256+
var json = @"[{""ShouldStayNull"":""null"", ""ShouldBeReplaced"": null}]";
257+
var result = Csv.Create(new CreateInput() { InputType = CreateInputType.Json, Delimiter = ";", Json = json }, new CreateOption() { ForceQuotesAroundValues = true });
258+
Assert.AreEqual(
259+
@"""ShouldStayNull"";""ShouldBeReplaced""
260+
""null"";""""
261+
", result.Csv);
251262
}
252263

253264
[Test]
@@ -257,24 +268,17 @@ public void TestNoQuotesOption()
257268
""foo"" : "" Normally I would have quotes "",
258269
""bar"" : ""I would not""
259270
}]";
260-
var result2 = Csv.Create(new CreateInput() { InputType = CreateInputType.Json, Delimiter = ";", Json = json }, new CreateOption() { NeverAddQuotesAroundValues = false });
261-
Assert.AreEqual(result2.Csv,
271+
var result2 = Csv.Create(new CreateInput() { InputType = CreateInputType.Json, Delimiter = ";", Json = json }, new CreateOption() { ForceQuotesAroundValues = false, NeverAddQuotesAroundValues = false });
272+
Assert.AreEqual(
262273
@"foo;bar
263274
"" Normally I would have quotes "";I would not
264-
");
275+
", result2.Csv);
265276

266277
var result1 = Csv.Create(new CreateInput() { InputType = CreateInputType.Json, Delimiter = ";", Json = json }, new CreateOption() { NeverAddQuotesAroundValues = true });
267-
Assert.AreEqual(result1.Csv,
278+
Assert.AreEqual(
268279
@"foo;bar
269280
Normally I would have quotes ;I would not
270-
");
271-
272-
273-
274-
275-
276-
277-
281+
", result1.Csv);
278282
}
279283

280284
[Test]
@@ -285,10 +289,10 @@ public void TestDatetimeValue()
285289
""string"" : ""foo""
286290
}]";
287291
var result = Csv.Create(new CreateInput() { InputType = CreateInputType.Json, Delimiter = ";", Json = json }, new CreateOption() { });
288-
Assert.AreEqual(result.Csv,
292+
Assert.AreEqual(
289293
@"datetime;string
290294
2018-11-22T10:30:55;foo
291-
");
295+
", result.Csv);
292296
}
293297

294298
[Test]
@@ -300,10 +304,10 @@ public void TestDecimalValues()
300304
""baz"" : 0.000000000000000000000000000000000000000000000000000000001
301305
}]";
302306
var result = Csv.Create(new CreateInput() { InputType = CreateInputType.Json, Delimiter = ";", Json = json }, new CreateOption() { });
303-
Assert.AreEqual(result.Csv,
307+
Assert.AreEqual(
304308
@"foo;bar;baz
305309
0.1;1.00;0.000000000000000000000000000000000000000000000000000000001
306-
");
310+
", result.Csv);
307311
}
308312

309313
[Test]
@@ -328,10 +332,10 @@ public void ParseAndWriteShouldUseSeparateCultures()
328332

329333
var result = Csv.Create(new CreateInput() { InputType = CreateInputType.List, Delimiter = ";", Data = parseResult.Data, Headers = parseResult.Headers }, new CreateOption() { CultureInfo = "fi-FI" });
330334

331-
Assert.AreEqual(result.Csv,
335+
Assert.AreEqual(
332336
@"First;Second;Number;Date
333337
Foo;"" bar"";100;1.1.2000 0.00.00
334-
");
338+
", result.Csv);
335339
}
336340

337341
[Test]

Frends.Csv/Csv.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,19 @@ public static CreateResult Create([PropertyTab] CreateInput input, [PropertyTab]
154154
HasHeaderRecord = option.IncludeHeaderRow
155155
};
156156

157+
if (option.ForceQuotesAroundValues)
158+
{
159+
config.ShouldQuote = (field) => true;
160+
}
161+
157162
if (option.NeverAddQuotesAroundValues)
158163
{
159164
config.Mode = CsvMode.NoEscape;
160165
// if IgnoreQuotes is true, seems like ShouldQuote function has to return false in all cases
161166
// if IgnoreQuotes is false ShouldQuote can't have any implementation otherwise it will overwrite IgnoreQuotes statement ( might turn it on again)
162-
config.ShouldQuote = (field) => (!option.NeverAddQuotesAroundValues);
167+
config.ShouldQuote = (field) => false;
163168
}
169+
164170
var csv = string.Empty;
165171

166172
switch (input.InputType)

Frends.Csv/Definitions.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,19 @@ public class CreateOption
6565
/// If set true csv's fields are never put in quotes
6666
/// </summary>
6767
[DefaultValue("false")]
68-
public bool NeverAddQuotesAroundValues { get; set; }
68+
public bool NeverAddQuotesAroundValues { get; set; } = false;
6969

7070
/// <summary>
7171
/// Input's null values will be replaced with this value
7272
/// </summary>
7373
[DisplayFormat(DataFormatString = "Text")]
7474
public string ReplaceNullsWith { get; set; }
75+
76+
/// <summary>
77+
/// Force quotes to all values
78+
/// </summary>
79+
[DefaultValue("false")]
80+
public bool ForceQuotesAroundValues { get; set; } = false;
7581
}
7682

7783
public class CreateResult
@@ -163,6 +169,7 @@ public class ParseOption
163169
/// </summary>
164170
[DefaultValue("false")]
165171
public bool TreatMissingFieldsAsNulls { get; set; } = false;
172+
166173
/// <summary>
167174
/// A flag to let the reader know if reference should be ignored.
168175
/// </summary>
@@ -175,6 +182,8 @@ public class ParseOption
175182
[DefaultValue("false")]
176183
public bool IgnoreQuotes { get; set; } = false;
177184

185+
186+
178187
}
179188

180189
public class ParseResult

Frends.Csv/Frends.Csv.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
5-
<Version>1.0.1</Version>
5+
<Version>1.0.0</Version>
66
<GenerateDocumentationFile>true</GenerateDocumentationFile>
77
<Company>HiQ Finland</Company>
88
<Authors>HiQ Finland</Authors>

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ NOTE: Be sure to merge the latest from "upstream" before making a pull request!
112112
| CultureInfo | string | The culture info to write the file with, e.g. for decimal separators. InvariantCulture will be used by default. See list of cultures [here](https://msdn.microsoft.com/en-us/library/ee825488(v=cs.20).aspx); use the Language Culture Name. <br> NOTE: Due to an issue with the CsvHelpers library, all CSV tasks will use the culture info setting of the first CSV task in the process; you cannot use different cultures for reading and parsing CSV files in the same process. |
113113
| NeverAddQuotesAroundValues | bool | If set true csv's fields are never put in quotes |
114114
| ReplaceNullsWith | string | Input's null values will be replaced with this value |
115+
| ForceQuotesAroundValues | bool | Force quotes around all values |
115116

116117
#### Result
117118

0 commit comments

Comments
 (0)