Exception on deserialization of IDictionary
See original GitHub issueHi there, I wrote a unit test against version 2.1.3 that show the exception:
using ExtendedXmlSerializer.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using Xunit;
namespace ExtendedXmlSerializer.Tests
{
public class Class1
{
[Fact]
public void TestDictionarySerialization()
{
var data = new Model();
data.Init();
var tempDir = Path.GetTempPath();
var tempFile = Path.Combine(tempDir, "test.xml");
var serializer = new ConfigurationContainer().Create();
using (XmlWriter writer = XmlWriter.Create(tempFile, new XmlWriterSettings { Indent = true }))
serializer.Serialize(writer, data);
using (XmlReader reader = XmlReader.Create(tempFile))
{
var result = (Model)serializer.Deserialize(reader);
}
}
}
#region Test Data
class Model
{
public int Id { get; set; }
public string Name { get; set; }
public IDictionary<string, string> Map { get; set; } = new Dictionary<string, string>();
public void Init()
{
Id = 2;
Name = "Parent";
Map = new Dictionary<string, string> { { "test", "test1" }, { "test2", "test2" } };
}
}
#endregion Test Data
}
the result test.xml
is as below :
<?xml version="1.0" encoding="utf-8"?>
<Model xmlns="clr-namespace:ExtendedXmlSerializer.Tests;assembly=ExtendedXmlSerializer.Tests">
<Id>2</Id>
<Name>Parent</Name>
<Map xmlns:sys="https://extendedxmlserializer.github.io/system" xmlns:exs="https://extendedxmlserializer.github.io/v2" exs:type="sys:Dictionary[sys:string,sys:string]">
<sys:Item>
<Key>test</Key>
<Value>test1</Value>
</sys:Item>
<sys:Item>
<Key>test2</Key>
<Value>test2</Value>
</sys:Item>
</Map>
</Model>
the serializer can not deserialize Map
property
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:6 (5 by maintainers)
Top Results From Across the Web
C# Deserialization exception on the object inherited from ...
In this case TryMe can be deserialized without deserialization constructor. Exception is not thrown.
Read more >System.Text.Json doesn't deserialize IDictionary with TKey ...
Text. Json. Deserialize() cannot deserialize to IDictionary where TKey has type other than string. This code above would throw exception System.
Read more >.Net Tips — Xml Serialize or Deserialize Dictionary in C# | ...
If you try to serialize/deserialize a type which uses the generic Dictionary type with the XmlSerializer then you'll get an InvalidOperationException, ...
Read more >How to deserialize JSON to Dictionary<string, class> ...
I'm querying an API that returns a JSON dictionary in which the keys cannot be hard coded into a C# class before-hand. The...
Read more >How to resolve .net core 3.1 json deserialization exception.
Dear Experts, Please help me to resolve this below exception. I am using .net core 3.1 System.Text.Json namespace, I don't want to use...
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
We should create documentation for extension methods. Our documentation is really poor 😉
Alright, awesome. Thank you for pointing out this issue @mjalil. It’s kind of embarrassing, TBH. 😄 All of our testing has been geared towards
XmlReader
s that have been created via theXmlReaderFactory
but we never tested the VERY OBVIOUS SCENARIO of creating them as traditionally done via theXmlReader.Create
. Luckily all the components used to do this were internal and this has been addressed with no breaking changes.This fix is now available via our preview feed at: https://www.myget.org/F/wojtpl2/api/v3/index.json
Please try it out and see how it treats you.
@wojtpl2 this might be something worth deploying sooner than later, if we can. I’m surprised it hasn’t been reported yet. I guess that shows how much everyone is using our extension methods. Or I’d like to think that, at least. 😆