Exception when deserializing List with generic classes
See original GitHub issueHello 😉
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:
- Created 5 years ago
- Comments:6 (5 by maintainers)
Top 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 >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
@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.
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.