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

Commit 700a42d

Browse files
committed
Allow UTF8Encoding to be configurable in all serializers
1 parent 5b74e9a commit 700a42d

3 files changed

Lines changed: 13 additions & 14 deletions

File tree

src/ServiceStack.Text/CsvSerializer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace ServiceStack.Text
1313
{
1414
public class CsvSerializer
1515
{
16-
private static readonly UTF8Encoding UTF8EncodingWithoutBom = new UTF8Encoding(false);
16+
public static UTF8Encoding UTF8Encoding = new UTF8Encoding(false); //Don't emit UTF8 BOM by default
1717

1818
private static Dictionary<Type, WriteObjectDelegate> WriteFnCache = new Dictionary<Type, WriteObjectDelegate>();
1919

@@ -87,15 +87,15 @@ public static void SerializeToWriter<T>(T value, TextWriter writer)
8787
public static void SerializeToStream<T>(T value, Stream stream)
8888
{
8989
if (value == null) return;
90-
var writer = new StreamWriter(stream, UTF8EncodingWithoutBom);
90+
var writer = new StreamWriter(stream, UTF8Encoding);
9191
CsvSerializer<T>.WriteObject(writer, value);
9292
writer.Flush();
9393
}
9494

9595
public static void SerializeToStream(object obj, Stream stream)
9696
{
9797
if (obj == null) return;
98-
var writer = new StreamWriter(stream, UTF8EncodingWithoutBom);
98+
var writer = new StreamWriter(stream, UTF8Encoding);
9999
var writeFn = GetWriteFn(obj.GetType());
100100
writeFn(writer, obj);
101101
writer.Flush();

src/ServiceStack.Text/JsonSerializer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace ServiceStack.Text
2525
/// </summary>
2626
public static class JsonSerializer
2727
{
28-
private static readonly UTF8Encoding UTF8EncodingWithoutBom = new UTF8Encoding(false);
28+
public static UTF8Encoding UTF8Encoding = new UTF8Encoding(false); //Don't emit UTF8 BOM by default
2929

3030
public static T DeserializeFromString<T>(string value)
3131
{
@@ -197,30 +197,30 @@ public static void SerializeToStream<T>(T value, Stream stream)
197197
}
198198
else
199199
{
200-
var writer = new StreamWriter(stream, UTF8EncodingWithoutBom);
200+
var writer = new StreamWriter(stream, UTF8Encoding);
201201
JsonWriter<T>.WriteRootObject(writer, value);
202202
writer.Flush();
203203
}
204204
}
205205

206206
public static void SerializeToStream(object value, Type type, Stream stream)
207207
{
208-
var writer = new StreamWriter(stream, UTF8EncodingWithoutBom);
208+
var writer = new StreamWriter(stream, UTF8Encoding);
209209
JsonWriter.GetWriteFn(type)(writer, value);
210210
writer.Flush();
211211
}
212212

213213
public static T DeserializeFromStream<T>(Stream stream)
214214
{
215-
using (var reader = new StreamReader(stream, UTF8EncodingWithoutBom))
215+
using (var reader = new StreamReader(stream, UTF8Encoding))
216216
{
217217
return DeserializeFromString<T>(reader.ReadToEnd());
218218
}
219219
}
220220

221221
public static object DeserializeFromStream(Type type, Stream stream)
222222
{
223-
using (var reader = new StreamReader(stream, UTF8EncodingWithoutBom))
223+
using (var reader = new StreamReader(stream, UTF8Encoding))
224224
{
225225
return DeserializeFromString(reader.ReadToEnd(), type);
226226
}

src/ServiceStack.Text/TypeSerializer.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
using System.Globalization;
1616
using System.IO;
1717
using System.Text;
18-
using System.Reflection;
1918
using ServiceStack.Text.Common;
2019
using ServiceStack.Text.Jsv;
2120

@@ -26,7 +25,7 @@ namespace ServiceStack.Text
2625
/// </summary>
2726
public static class TypeSerializer
2827
{
29-
private static readonly UTF8Encoding UTF8EncodingWithoutBom = new UTF8Encoding(false);
28+
public static UTF8Encoding UTF8Encoding = new UTF8Encoding(false); //Don't emit UTF8 BOM by default
3029

3130
public const string DoubleQuoteString = "\"\"";
3231

@@ -212,15 +211,15 @@ public static void SerializeToStream<T>(T value, Stream stream)
212211
}
213212
else
214213
{
215-
var writer = new StreamWriter(stream, UTF8EncodingWithoutBom);
214+
var writer = new StreamWriter(stream, UTF8Encoding);
216215
JsvWriter<T>.WriteRootObject(writer, value);
217216
writer.Flush();
218217
}
219218
}
220219

221220
public static void SerializeToStream(object value, Type type, Stream stream)
222221
{
223-
var writer = new StreamWriter(stream, UTF8EncodingWithoutBom);
222+
var writer = new StreamWriter(stream, UTF8Encoding);
224223
JsvWriter.GetWriteFn(type)(writer, value);
225224
writer.Flush();
226225
}
@@ -234,15 +233,15 @@ public static T Clone<T>(T value)
234233

235234
public static T DeserializeFromStream<T>(Stream stream)
236235
{
237-
using (var reader = new StreamReader(stream, UTF8EncodingWithoutBom))
236+
using (var reader = new StreamReader(stream, UTF8Encoding))
238237
{
239238
return DeserializeFromString<T>(reader.ReadToEnd());
240239
}
241240
}
242241

243242
public static object DeserializeFromStream(Type type, Stream stream)
244243
{
245-
using (var reader = new StreamReader(stream, UTF8EncodingWithoutBom))
244+
using (var reader = new StreamReader(stream, UTF8Encoding))
246245
{
247246
return DeserializeFromString(reader.ReadToEnd(), type);
248247
}

0 commit comments

Comments
 (0)