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.

Exception when deserializing List with generic classes

See original GitHub issue

Hello 😉

I think there is something wrong. Could you please review my unit test ?

Thanks!

using ExtendedXmlSerializer.Configuration;
using ExtendedXmlSerializer.ExtensionModel.Xml;
using ExtendedXmlSerializer.Tests.Support;
using FluentAssertions;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace ExtendedXmlSerializer.Tests.ReportedIssues
{
    public sealed class Issue194Tests
    {
        interface IDescribable
        {
            string Description { get; set; }
        }

        class ClassWithGeneric<T> : IDescribable
        {
            public string Description { get; set; }
        }

        [Fact]
        void GenericSimpleValue()
        {
            var serializer = new ConfigurationContainer()
                                                .Create()
                                                .ForTesting();

            ClassWithGeneric<int> singleValue = new ClassWithGeneric<int>();
            singleValue.Description = "testtest123";

            string result = serializer.Serialize(singleValue);


            var deserializedValue = serializer.Deserialize<ClassWithGeneric<int>>(result);

            deserializedValue.Description.Should().Be("testtest123");
        }

        [Fact]
        void GenericListValues()
        {
            var serializer = new ConfigurationContainer()
                                                .Create()
                                                .ForTesting();

            ClassWithGeneric<int> singleValue = new ClassWithGeneric<int>();
            singleValue.Description = "testtest345";

            List<IDescribable> list = new List<IDescribable>()
            {
                singleValue
            };

            string result = serializer.Serialize(list);

            var deserializedValue = serializer.Deserialize<List<IDescribable>>(result);

            deserializedValue.First().Description.Should().Be("testtest345");
        }

        [Fact]
        void GenericLinkedListValues()
        {
            var serializer = new ConfigurationContainer()
                                                .Create()
                                                .ForTesting();

            ClassWithGeneric<int> singleValue = new ClassWithGeneric<int>();
            singleValue.Description = "testtest345";

            LinkedList<IDescribable> list = new LinkedList<IDescribable>();
            list.AddFirst(singleValue);

            string result = serializer.Serialize(list);

            var deserializedValue = serializer.Deserialize<LinkedList<IDescribable>>(result);

            deserializedValue.First().Description.Should().Be("testtest345");
        }

        [Fact]
        void GenericDictionaryValues()
        {
            var serializer = new ConfigurationContainer()
                                                .Create()
                                                .ForTesting();

            ClassWithGeneric<int> singleValue = new ClassWithGeneric<int>();
            singleValue.Description = "testtest345";

            IDictionary<string, IDescribable> list = new Dictionary<string, IDescribable>()
            {
                {"key", singleValue }
            };

            string result = serializer.Serialize(list);

            var deserializedValue = serializer.Deserialize<Dictionary<string, IDescribable>>(result);

            deserializedValue.First().Value.Description.Should().Be("testtest345");
        }


    }
}

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
WojciechNagorskicommented, Aug 20, 2018

@Mike-EEE Great job! I don’t see any problem. You can create a new build if you like or I can do it. Please let me know if I should do it.

0reactions
Mike-E-angelocommented, Aug 20, 2018

OK cool… nope, I should get some experience in that. 😄 I went ahead and pushed 2.1.4. Let’s hope it works! 😂 Closing this issue for now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to deserialize generic List<T> with Jackson?
List ; public class JsonTypeApp { public static void main(String[] args) throws Exception { File jsonFile = new File("./resource/test.json").
Read more >
Deserialization Not Working Right with Generic Types and ...
When trying to deserialize a generic type using a builder it is deserializing the generic type as a LinkedHashMap instead of the proper...
Read more >
Deserialize Generic Type with Jackson
Explore two simple ways to deserialize a JSON string into an object with a generic type.
Read more >
How to deserialize a JSON array to list generic type in Java
How to deserialize a JSON array to list generic type in Java - The Gson library provides a class called com.google.gson.reflect.
Read more >
How to Serialize and Deserialize Generic Java Classes with ...
If you want to deserialize to MyExchange<MyBody> as in the following code, you will get a ClassCastException . MyExchange<MyBody> myDeserializedExchange = ...
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