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.

Serialization behavior

See original GitHub issue

I’m starting integrate ExtendedXmlSerializer to my pet project instead of standard System.Xml. And I have some troubles. Can you suggest how to be in the next situation:

Look at the small repro

    internal class Program
    {
        public static void Main(string[] args)
        {
            var xs = new ConfigurationContainer().Create();
            using (var xw = (XmlWriter.Create(Console.Out)))
                xs.Serialize(xw, new SerializedObject());
        }
    }

    public class MyListImpl<T> : List<T>
    {
        private readonly object _owner;

        public MyListImpl(object owner)
        {
            _owner = owner;
        }
    }

    public class SerializedObject
    {
        public SerializedObject()
        {
            MyListImpl = new MyListImpl<string>(this);
            MyListImpl.Add("Test");
        }

        public MyListImpl<string> MyListImpl { get; }
    }

I have class look like SerializedObject (it has the read-only custom list with generic argument property that have not parameter less constructor), but the collection instantiate during SerializedObject construct. It works like a charm in System.Xml but it throw the exception then I try serialize it with default settings using the ExtendedXmlSerializer.

 System.InvalidOperationException: The serializer for type 'ProjectForTests.MyListImpl`1[System.String]' 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.

I have a lot of places with the same code and I can’t and parameter less constructor (I have to guarantee the client what the collection know about the parent).

How can I say to the EXS: “If property type like this: ProjectForTests.MyListImpl<TSomeType> please don’t try to create this. The property already created by the owner instance.”

Thank you.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
Mike-E-angelocommented, Oct 26, 2018

Ah! A PR with failing test is EVEN BETTER than that, @cm4ker. 😆 I had to brush off how to do PRs but I was finally able to load your code. Thank you for providing it.

As I mentioned earlier, the parameterized content (by default) (follows protobuf serialization rules found here.

However, these rules are not obvious in the exception that is thrown. So I have created a new issue, using your PR as the base to ensure an exception message is thrown now that details these rules. In your case, the ChildList class should look like this:

https://github.com/wojtpl2/ExtendedXmlSerializer/blob/d3a582b4d098f0d8aa88c03ec505dc65fa8ec661/test/ExtendedXmlSerializer.Tests/ReportedIssues/Issue218Tests.cs#L89-L94

Here’s the test that shows this passing: https://github.com/wojtpl2/ExtendedXmlSerializer/blob/d3a582b4d098f0d8aa88c03ec505dc65fa8ec661/test/ExtendedXmlSerializer.Tests/ReportedIssues/Issue218Tests.cs#L57-L70

The other item of note is that this did yield a bug that public fields were not working. 😃 So this has been fixed and you will need to use the preview feed to see it in action: https://www.myget.org/F/wojtpl2/api/v3/index.json

Please let me know if you have any further questions around this and I will do my best to further assist.

1reaction
cm4kercommented, Oct 17, 2018

Yea this is exact what I want. Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to serialize and deserialize JSON using C# - .NET
Deserialization behavior. The following behaviors apply when deserializing JSON: By default, property name matching is case-sensitive. You can ...
Read more >
Serialization behavior changed in 2.14 when mixing ...
Serialization behavior changed in 2.14 when mixing @JsonIgnore and @JsonProperty on different levels of class hierarchy and/or accessors # ...
Read more >
Introduction to Java Serialization
The serialization process is instance-independent; for example, we can serialize objects on one platform and deserialize them on another.
Read more >
Serialization in Java
Serialization in Java was introduced in JDK 1.1 and it is one of the important feature of Core Java. Serialization in Java.
Read more >
Serialization Attributes
NET's default behavior) or fields (all public and private fields are serialized and properties are ignored). Placing the the DataContractAttribute on a type...
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