How can I serialize `Encoding` properties
See original GitHub issueI have a class that contains a System.Text.Encoding
property
public class ClassWithEncodingProperty
{
public Encoding Encoding { get; set; }
public ClassWithEncodingProperty()
{
this.Encoding = Encoding.Default;
}
}
And a test to serialize this class:
public class ClassWithEncodingPropertySpec
{
[Fact]
public void TestSerialize()
{
var c = new ClassWithEncodingProperty();
var serializerContainer = new ConfigurationContainer()
.ConfigureType<ClassWithEncodingProperty>();
var serializer = serializerContainer.Create();
var stream = new MemoryStream();
using (var writer = XmlWriter.Create(stream))
{
serializer.Serialize(writer, c);
writer.Flush();
}
stream.Seek(0, SeekOrigin.Begin);
string contents = new StreamReader(stream).ReadToEnd();
}
}
An exception is thrown when I try to serialize this class:
Message: System.InvalidOperationException : The serializer for type 'System.Text.SBCSCodePageEncoding' could not be found. Please ensure that the type is a valid type can be activated. The default behavior requires an empty public constructor on the (non-abstract) class to activate.
Since ‘System.Text.SBCSCodePageEncoding’ is internal
I can’t create a, IExtendedXmlCustomSerializer
directly, so I did the next best thing I could come up with and use System.Text.Encoding
, which is the superclass of SBCSCodePageEncoding
.
public class EncodingSerializer : IExtendedXmlCustomSerializer<Encoding>
{
public Encoding Deserialize(XElement xElement)
{
return Encoding.UTF8; // Needs to be replace with real deserialization once the error is gone.
}
public void Serializer(XmlWriter xmlWriter, Encoding obj)
{
xmlWriter.WriteString("Replace this with real encoding"); // Needs to be replaced with real serialization once the error is gone.
}
}
And I registered the custom serializer in the ConfigurationContainer
:
public class ClassWithEncodingPropertySpec
{
[Fact]
public void TestSerialize()
{
var c = new ClassWithEncodingProperty();
var serializerContainer = new ConfigurationContainer()
.ConfigureType<Encoding>()
.CustomSerializer(new EncodingSerializer())
.ConfigureType<ClassWithEncodingProperty>();
// Omitted for brevity
}
}
But this still throws the same error.
So how can I serialize this internal
subclass.
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
How to customize character encoding with System.Text.Json
To serialize all language sets without escaping, use UnicodeRanges.All. Serialize specific characters. An alternative is to specify individual ...
Read more >How can I serialize `Encoding` properties · Issue #253
I have a class that contains a System.Text.Encoding property public class ClassWithEncodingProperty { public Encoding Encoding { get; set; } ...
Read more >c# - Specify properties and field to encode and decode with ...
You can do this by relying on the Conditional Property Serialization capability of Newtonsoft. All you need to do is to create a...
Read more >Encoding, Decoding, and Serialization
Serialize and deserialize instances of your types with implicit or customized ... making the encoded properties of an encodable type accessible by keys....
Read more >String Encoding / Decoding for kotlinx.serialization.properties
Using the class kotlinx.serialization.properties.Properties , you can serialize to / deserialize from a Map<String, Any> , which is (supposedly, again ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have not been able to reproduce it either. Very weird, I did restart my computer, though.
This has been deployed to Nuget: https://www.nuget.org/packages/ExtendedXmlSerializer/
If you run into any additional problems please feel free to open a new issue.
Closing for now.