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.

Deserializer Can't find a Class Program-ClassA (Should be Program.ClassA)

See original GitHub issue

Running the below code results in the following error “ExtendedXmlSerializer.Core.Sprache.ParseException: ‘An attempt was made to parse the identity ‘clr-namespace:;assembly=StackTypeSerialization:Program-ClassA’, but no type could be located with that name.’”. using release 2.1.15 targeting .Net Core 2.1

using ExtendedXmlSerializer.Configuration;
using System.Collections.Generic;
using System.Xml;

public class Program
{
    public static void Main()
    {
        var outList = new List<ClassA>();

        var classB = new ClassB();
        var classC = new ClassC();

        outList.Add(new ClassA()
        {
            blah = "Test1",
            InterfaceConcreteTypeA = new ClassB() { TestInterfaceProperty = "Blah1", TestConcretePropertyB = "Blah1" },
            InterfaceConcreteTypeB = new ClassC() { TestInterfaceProperty = "Blah2", TestConcretePropertyC = "Blah2" }
        });
        outList.Add(new ClassA()
        {
            blah = "Test2",
            InterfaceConcreteTypeA = new ClassB(),
            InterfaceConcreteTypeB = new ClassC()
        });
        outList.Add(new ClassA()
        {
            blah = "Test3",
            InterfaceConcreteTypeA = new ClassB() { TestInterfaceProperty = "Blah3", TestConcretePropertyB = "Blah3" },
            InterfaceConcreteTypeB = new ClassC() { TestInterfaceProperty = "Blah3", TestConcretePropertyC = "Blah3" }
        });


        var serializer = new ConfigurationContainer().Create();

        XmlWriterSettings settings = new XmlWriterSettings
        {
            Indent = true,
            IndentChars = "\t"
        };

        using (XmlWriter writer = XmlWriter.Create("Test.xml", settings))
        {
            serializer.Serialize(writer, outList.ToArray());
        }

        List<ClassA> inList = new List<ClassA>();

        using (XmlReader reader = XmlReader.Create("Test.xml", new XmlReaderSettings { IgnoreWhitespace = false }))
        {
            inList = (List<ClassA>)serializer.Deserialize(reader);
        }

    }

    
    public class ClassA
    {        
        public string blah { get; set; }
        public InterfaceA InterfaceConcreteTypeA { get; set; }
        public InterfaceA InterfaceConcreteTypeB { get; set; }
    }

    public class ClassB : InterfaceA
    {
        public string TestInterfaceProperty { get; set; }
        public string TestConcretePropertyB { get; set; }
    }
    public class ClassC : InterfaceA
    {
        public string TestInterfaceProperty { get ; set; }
        public string TestConcretePropertyC { get; set; }
    }

    public interface InterfaceA
    {
        string TestInterfaceProperty { get; set; }
    }

}

ctrls-sb-ben 2019-0130 11-35-05

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
TheColonel2688commented, Feb 4, 2019

So I tried the same thing with .net 4.5. Same result.

Then I tried moving all of the classes to their own file. GUESS WHAT!!! it worked… this does not make ANY SENSE. It should not matter what file they are in.

Then I thought, wait a minute there is no enclosing namespace {} in the main program.cs file… So I added one and put the classes back in that file and it worked…

qwbz74a

1reaction
TheColonel2688commented, Feb 4, 2019

for posterity, this is complete working code

I am not sure what you mean by “secretly failing without telling me”, it was definitely throwing an error on deserialize.


using ExtendedXmlSerializer.Configuration;
using System.Collections.Generic;
using System.Xml;
using ExtendedXmlSerializer.ExtensionModel.Xml;

namespace QuestionTypeSerialization
{

    public class Program
    {
        public static void Main()
        {

            var outList = new List<ClassA>();

            var classB = new ClassB();
            var classC = new ClassC();

            outList.Add(new ClassA()
            {
                blah = "Test1",
                InterfaceConcreteTypeA = new ClassB() { TestInterfaceProperty = "Blah1", TestConcretePropertyB = "Blah1" },
                InterfaceConcreteTypeB = new ClassC() { TestInterfaceProperty = "Blah2", TestConcretePropertyC = "Blah2" }
            });
            outList.Add(new ClassA()
            {
                blah = "Test2",
                InterfaceConcreteTypeA = new ClassB(),
                InterfaceConcreteTypeB = new ClassC()
            });
            outList.Add(new ClassA()
            {
                blah = "Test3",
                InterfaceConcreteTypeA = new ClassB() { TestInterfaceProperty = "Blah3", TestConcretePropertyB = "Blah3" },
                InterfaceConcreteTypeB = new ClassC() { TestInterfaceProperty = "Blah3", TestConcretePropertyC = "Blah3" }
            });


            var serializer = new ConfigurationContainer().Create();

            XmlWriterSettings settings = new XmlWriterSettings
            {
                Indent = true,
                IndentChars = "\t"
            };

            using (var xmlWriter = XmlWriter.Create("test.xml", settings))
            {
                serializer.Serialize(xmlWriter, outList);
            }


            var readerSettings = new XmlReaderSettings { IgnoreWhitespace = false };

            using (var xmlreader = XmlReader.Create("test.xml", readerSettings))
            {
                var blah = (List<ClassA>)serializer.Deserialize(xmlreader);
            }

        }
    }

    public class ClassA
    {
        public string blah { get; set; }
        public InterfaceA InterfaceConcreteTypeA { get; set; }
        public InterfaceA InterfaceConcreteTypeB { get; set; }
    }


    public class ClassB : InterfaceA
    {
        public string TestInterfaceProperty { get; set; }
        public string TestConcretePropertyB { get; set; }
    }
    public class ClassC : InterfaceA
    {
        public string TestInterfaceProperty { get; set; }
        public string TestConcretePropertyC { get; set; }
    }

    public interface InterfaceA
    {
        string TestInterfaceProperty { get; set; }
    }

}
Read more comments on GitHub >

github_iconTop Results From Across the Web

JSON.NET won't deserialize class correctly
The property names of that class match the ctor argument names, so I cannot understand why I get wrong values in there during...
Read more >
Should custom deserialization happen in a constructor or ...
No, it should not. Serialization is orthogonal to the object and thus should be kept outside. Custom serialization belongs in a ...
Read more >
Getting Started with Custom Deserialization in Jackson
This quick tutorial will illustrate how to use Jackson 2 to deserialize JSON using a custom Deserializer.
Read more >
How to serialize and deserialize JSON using C# - .NET
Copy the JSON that you need to deserialize. Create a class file and delete the template code. Choose Edit > Paste Special >...
Read more >
How to serialize properties of derived classes with System. ...
In this article, you will learn how to serialize properties of derived classes with the System.Text.Json namespace.
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