question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I serialize `Encoding` properties

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
johanvergeercommented, Jul 5, 2019

I have not been able to reproduce it either. Very weird, I did restart my computer, though.

0reactions
Mike-E-angelocommented, Jul 8, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found