Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 5e1300a

Browse files
committed
Strip quotes from JSV String values when serializing values in CSV Serializer
1 parent 8c15dba commit 5e1300a

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

src/ServiceStack.Text/CsvWriter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,9 @@ public static void Write(TextWriter writer, IEnumerable<T> records)
271271
var propertyGetter = PropertyGetters[i];
272272
var value = propertyGetter(record) ?? "";
273273

274-
var strValue = value.GetType() == typeof(string)
274+
var strValue = value is string
275275
? (string)value
276-
: TypeSerializer.SerializeToString(value);
276+
: TypeSerializer.SerializeToString(value).StripQuotes();
277277

278278
row[i] = strValue;
279279
}

src/ServiceStack.Text/StringExtensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,15 @@ public static string StripHtml(this string html)
541541
return String.IsNullOrEmpty(html) ? null : StripHtmlRegEx.Replace(html, "");
542542
}
543543

544+
public static string StripQuotes(this string text)
545+
{
546+
return string.IsNullOrEmpty(text) || text.Length < 2
547+
? text
548+
: text[0] == '"' && text[text.Length - 1] == '"'
549+
? text.Substring(1, text.Length - 2)
550+
: text;
551+
}
552+
544553
static readonly Regex StripBracketsRegEx = new Regex(@"\[(.|\n)*?\]", PclExport.Instance.RegexOptions);
545554
static readonly Regex StripBracesRegEx = new Regex(@"\((.|\n)*?\)", PclExport.Instance.RegexOptions);
546555

tests/ServiceStack.Text.Tests/JsonTests/CustomRawSerializerTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public class Response
202202
[Test]
203203
public void Can_serialize_custom_DateTime()
204204
{
205-
JsConfig<DateTime>.RawSerializeFn = time =>
205+
JsConfig<DateTime>.SerializeFn = time =>
206206
{
207207
var result = time;
208208
if (time.Kind == DateTimeKind.Unspecified)
@@ -215,8 +215,10 @@ public void Can_serialize_custom_DateTime()
215215
var dto = new Response { DateTime = new DateTime(2001, 1, 1, 1, 1, 1) };
216216

217217
var csv = dto.ToCsv();
218-
219218
Assert.That(csv, Is.EqualTo("DateTime\r\n01/01/2001 01:01:01\r\n"));
219+
220+
var json = dto.ToJson();
221+
Assert.That(json, Is.EqualTo("{\"DateTime\":\"01/01/2001 01:01:01\"}"));
220222
}
221223
}
222224

0 commit comments

Comments
 (0)