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 on deserialization of IDictionary

See original GitHub issue

Hi 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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
WojciechNagorskicommented, May 8, 2018

We should create documentation for extension methods. Our documentation is really poor 😉

1reaction
Mike-E-angelocommented, May 7, 2018

Alright, awesome. Thank you for pointing out this issue @mjalil. It’s kind of embarrassing, TBH. 😄 All of our testing has been geared towards XmlReaders that have been created via the XmlReaderFactory but we never tested the VERY OBVIOUS SCENARIO of creating them as traditionally done via the XmlReader.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. 😆

Read more comments on GitHub >

github_iconTop 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 >

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