Should I worry about coding during serialization?

public string Serialize(BackgroundJobInfo info) { var stringBuilder = new StringBuilder(); using (var stringWriter = new StringWriter(stringBuilder, CultureInfo.InvariantCulture)) { var writer = XmlWriter.Create(stringWriter); ... 

By default, StringWriter will declare itself as UTF-16 . Usually XML is in UTF-8 . So I can fix this by subclassing StringWriter

 public class Utf8StringWriter : StringWriter { public override Encoding Encoding { get { return Encoding.UTF8; } } } 

But why should I worry about this? What happens if I decide to use StringWriter (as I did) instead of Utf8StringWriter ? Will I have some kind of mistake?

After that I will write this line in MongoDb

+7
c #
source share
1 answer

StringWriter Encoding property is actually not that useful since it is based on a StringBuilder that creates .Net strings .. Net strings are encoded inside utf16, but these are implementation details that you need not worry about. Encoding is just a property inherited from TextWriter , because TextWriter can potentially write objects where the encoding matters ( Stream , byte[] , ...).

You end up with a plain old string . The encoding that you will use to serialize this string later is still not fixed, and if you use the MongoDb client implementation that takes the string as an argument, this is not even your problem!


On the other hand, overriding the getter of the Encoding property will not change the encoding method, even if the encoding was actually involved in StringWriter

+3
source share

All Articles