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

Commit 442bd10

Browse files
committed
Make CsvSerializer.UseEncoding overridable
1 parent 7fa7e16 commit 442bd10

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/ServiceStack.Text/CsvSerializer.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ namespace ServiceStack.Text
1515
{
1616
public class CsvSerializer
1717
{
18-
public static UTF8Encoding UTF8Encoding = new UTF8Encoding(false); //Don't emit UTF8 BOM by default
18+
//Don't emit UTF8 BOM by default
19+
public static Encoding UseEncoding { get; set; } = new UTF8Encoding(false);
1920

2021
private static Dictionary<Type, WriteObjectDelegate> WriteFnCache = new Dictionary<Type, WriteObjectDelegate>();
2122
internal static WriteObjectDelegate GetWriteFn(Type type)
@@ -116,15 +117,15 @@ public static void SerializeToWriter<T>(T value, TextWriter writer)
116117
public static void SerializeToStream<T>(T value, Stream stream)
117118
{
118119
if (value == null) return;
119-
var writer = new StreamWriter(stream, UTF8Encoding);
120+
var writer = new StreamWriter(stream, UseEncoding);
120121
CsvSerializer<T>.WriteObject(writer, value);
121122
writer.Flush();
122123
}
123124

124125
public static void SerializeToStream(object obj, Stream stream)
125126
{
126127
if (obj == null) return;
127-
var writer = new StreamWriter(stream, UTF8Encoding);
128+
var writer = new StreamWriter(stream, UseEncoding);
128129
var writeFn = GetWriteFn(obj.GetType());
129130
writeFn(writer, obj);
130131
writer.Flush();
@@ -133,7 +134,7 @@ public static void SerializeToStream(object obj, Stream stream)
133134
public static T DeserializeFromStream<T>(Stream stream)
134135
{
135136
if (stream == null) return default(T);
136-
using (var reader = new StreamReader(stream, UTF8Encoding))
137+
using (var reader = new StreamReader(stream, UseEncoding))
137138
{
138139
return DeserializeFromString<T>(reader.ReadToEnd());
139140
}
@@ -142,7 +143,7 @@ public static T DeserializeFromStream<T>(Stream stream)
142143
public static object DeserializeFromStream(Type type, Stream stream)
143144
{
144145
if (stream == null) return null;
145-
using (var reader = new StreamReader(stream, UTF8Encoding))
146+
using (var reader = new StreamReader(stream, UseEncoding))
146147
{
147148
return DeserializeFromString(type, reader.ReadToEnd());
148149
}

0 commit comments

Comments
 (0)