From 52f06fe14edbe5b50d14bda4cc5e13e48994174e Mon Sep 17 00:00:00 2001 From: Albie Date: Sat, 21 Aug 2021 20:24:28 +0100 Subject: [PATCH] add back encoding and disable utf8 bom --- DragonFruit.Common.Data/Serializers/ApiJsonSerializer.cs | 4 ++-- DragonFruit.Common.Data/Serializers/ApiXmlSerializer.cs | 4 ++-- DragonFruit.Common.Data/Utils/SerializerUtils.cs | 8 +++++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/DragonFruit.Common.Data/Serializers/ApiJsonSerializer.cs b/DragonFruit.Common.Data/Serializers/ApiJsonSerializer.cs index eb9b726..6386bd6 100644 --- a/DragonFruit.Common.Data/Serializers/ApiJsonSerializer.cs +++ b/DragonFruit.Common.Data/Serializers/ApiJsonSerializer.cs @@ -64,7 +64,7 @@ private ApiJsonSerializer(Encoding encoding, bool autoDetectEncoding) public Encoding Encoding { - get => _encoding ?? Encoding.UTF8; + get => _encoding ?? new UTF8Encoding(false); set => _encoding = value; } @@ -83,7 +83,7 @@ public HttpContent Serialize(T input) where T : class Serializer.Serialize(jsonWriter, input); } - return SerializerUtils.ProcessStream(this, stream, Encoding); + return SerializerUtils.ProcessStream(this, stream); } public T Deserialize(Stream input) where T : class diff --git a/DragonFruit.Common.Data/Serializers/ApiXmlSerializer.cs b/DragonFruit.Common.Data/Serializers/ApiXmlSerializer.cs index abaa2c5..13f2ef1 100644 --- a/DragonFruit.Common.Data/Serializers/ApiXmlSerializer.cs +++ b/DragonFruit.Common.Data/Serializers/ApiXmlSerializer.cs @@ -23,7 +23,7 @@ public ApiXmlSerializer(Encoding encoding = null, bool autoDetectEncoding = true public Encoding Encoding { - get => _encoding ?? Encoding.UTF8; + get => _encoding ?? new UTF8Encoding(false); set => _encoding = value; } @@ -38,7 +38,7 @@ public HttpContent Serialize(T input) where T : class new XmlSerializer(typeof(T)).Serialize(writer, input); } - return SerializerUtils.ProcessStream(this, stream, Encoding); + return SerializerUtils.ProcessStream(this, stream); } public T Deserialize(Stream input) where T : class diff --git a/DragonFruit.Common.Data/Utils/SerializerUtils.cs b/DragonFruit.Common.Data/Utils/SerializerUtils.cs index 15cf23a..f24e896 100644 --- a/DragonFruit.Common.Data/Utils/SerializerUtils.cs +++ b/DragonFruit.Common.Data/Utils/SerializerUtils.cs @@ -4,20 +4,22 @@ using System.IO; using System.Net.Http; using System.Net.Http.Headers; -using System.Text; using DragonFruit.Common.Data.Serializers; namespace DragonFruit.Common.Data.Utils { public static class SerializerUtils { - public static HttpContent ProcessStream(ISerializer serializer, Stream stream, Encoding encoding) + public static HttpContent ProcessStream(ISerializer serializer, Stream stream) { stream.Seek(0, SeekOrigin.Begin); var content = new StreamContent(stream); content.Headers.ContentLength = stream.Length; - content.Headers.ContentType = new MediaTypeHeaderValue(serializer.ContentType); + content.Headers.ContentType = new MediaTypeHeaderValue(serializer.ContentType) + { + CharSet = serializer.Encoding.HeaderName + }; return content; }