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

Commit

Permalink
Allow UTF8Encoding to be configurable in all serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Feb 4, 2015
1 parent 5b74e9a commit 700a42d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/ServiceStack.Text/CsvSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace ServiceStack.Text
{
public class CsvSerializer
{
private static readonly UTF8Encoding UTF8EncodingWithoutBom = new UTF8Encoding(false);
public static UTF8Encoding UTF8Encoding = new UTF8Encoding(false); //Don't emit UTF8 BOM by default

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

Expand Down Expand Up @@ -87,15 +87,15 @@ public static void SerializeToWriter<T>(T value, TextWriter writer)
public static void SerializeToStream<T>(T value, Stream stream)
{
if (value == null) return;
var writer = new StreamWriter(stream, UTF8EncodingWithoutBom);
var writer = new StreamWriter(stream, UTF8Encoding);
CsvSerializer<T>.WriteObject(writer, value);
writer.Flush();
}

public static void SerializeToStream(object obj, Stream stream)
{
if (obj == null) return;
var writer = new StreamWriter(stream, UTF8EncodingWithoutBom);
var writer = new StreamWriter(stream, UTF8Encoding);
var writeFn = GetWriteFn(obj.GetType());
writeFn(writer, obj);
writer.Flush();
Expand Down
10 changes: 5 additions & 5 deletions src/ServiceStack.Text/JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace ServiceStack.Text
/// </summary>
public static class JsonSerializer
{
private static readonly UTF8Encoding UTF8EncodingWithoutBom = new UTF8Encoding(false);
public static UTF8Encoding UTF8Encoding = new UTF8Encoding(false); //Don't emit UTF8 BOM by default

public static T DeserializeFromString<T>(string value)
{
Expand Down Expand Up @@ -197,30 +197,30 @@ public static void SerializeToStream<T>(T value, Stream stream)
}
else
{
var writer = new StreamWriter(stream, UTF8EncodingWithoutBom);
var writer = new StreamWriter(stream, UTF8Encoding);
JsonWriter<T>.WriteRootObject(writer, value);
writer.Flush();
}
}

public static void SerializeToStream(object value, Type type, Stream stream)
{
var writer = new StreamWriter(stream, UTF8EncodingWithoutBom);
var writer = new StreamWriter(stream, UTF8Encoding);
JsonWriter.GetWriteFn(type)(writer, value);
writer.Flush();
}

public static T DeserializeFromStream<T>(Stream stream)
{
using (var reader = new StreamReader(stream, UTF8EncodingWithoutBom))
using (var reader = new StreamReader(stream, UTF8Encoding))
{
return DeserializeFromString<T>(reader.ReadToEnd());
}
}

public static object DeserializeFromStream(Type type, Stream stream)
{
using (var reader = new StreamReader(stream, UTF8EncodingWithoutBom))
using (var reader = new StreamReader(stream, UTF8Encoding))
{
return DeserializeFromString(reader.ReadToEnd(), type);
}
Expand Down
11 changes: 5 additions & 6 deletions src/ServiceStack.Text/TypeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
using System.Globalization;
using System.IO;
using System.Text;
using System.Reflection;
using ServiceStack.Text.Common;
using ServiceStack.Text.Jsv;

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

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

Expand Down Expand Up @@ -212,15 +211,15 @@ public static void SerializeToStream<T>(T value, Stream stream)
}
else
{
var writer = new StreamWriter(stream, UTF8EncodingWithoutBom);
var writer = new StreamWriter(stream, UTF8Encoding);
JsvWriter<T>.WriteRootObject(writer, value);
writer.Flush();
}
}

public static void SerializeToStream(object value, Type type, Stream stream)
{
var writer = new StreamWriter(stream, UTF8EncodingWithoutBom);
var writer = new StreamWriter(stream, UTF8Encoding);
JsvWriter.GetWriteFn(type)(writer, value);
writer.Flush();
}
Expand All @@ -234,15 +233,15 @@ public static T Clone<T>(T value)

public static T DeserializeFromStream<T>(Stream stream)
{
using (var reader = new StreamReader(stream, UTF8EncodingWithoutBom))
using (var reader = new StreamReader(stream, UTF8Encoding))
{
return DeserializeFromString<T>(reader.ReadToEnd());
}
}

public static object DeserializeFromStream(Type type, Stream stream)
{
using (var reader = new StreamReader(stream, UTF8EncodingWithoutBom))
using (var reader = new StreamReader(stream, UTF8Encoding))
{
return DeserializeFromString(reader.ReadToEnd(), type);
}
Expand Down

0 comments on commit 700a42d

Please sign in to comment.