Bug 14186 - DataContractJsonSerializer fails to deserialize IDictionary
Summary: DataContractJsonSerializer fails to deserialize IDictionary
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: 2.8.x
Hardware: All All
: --- normal
Target Milestone: Untriaged
Assignee: Rodrigo Kumpera
URL:
Depends on:
Blocks:
 
Reported: 2013-08-22 12:13 UTC by Bryan Crotaz
Modified: 2016-04-14 01:02 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
NUnit test case for json deserialization (2.90 KB, text/plain)
2013-08-22 12:20 UTC, Bryan Crotaz
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Bryan Crotaz 2013-08-22 12:13:50 UTC
The following code fails for DataWithIDictionary and DataWithIDictionaryProperty

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;

namespace TestMonoSerialization
{
    [Serializable]
    class Data
    {
        public string Name;
    }

    [Serializable]
    class DataWithList
    {
        public IList<string> List;
    }

    [Serializable]
    class DataWithDictionary
    {
        public Dictionary<string, string> Dictionary;
    }

    [Serializable]
    class DataWithIDictionary
    {
        public IDictionary<string, string> Dictionary;
    }

    [Serializable]
    class DataWithDictionaryProperty
    {
        public Dictionary<string, string> Dictionary { get; set; }
    }

    [Serializable]
    class DataWithIDictionaryProperty
    {
        public IDictionary<string, string> Dictionary { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Testing plain object");
            Test(new Data());
            Console.WriteLine("Testing object with child list");
            Test(new DataWithList());
            Console.WriteLine("Testing object with child dictionary");
            Test(new DataWithDictionary());
            Console.WriteLine("Testing object with child IDictionary");
            Test(new DataWithIDictionary());
            Console.WriteLine("Testing object with child dictionary property");
            Test(new DataWithDictionaryProperty());
            Console.WriteLine("Testing object with child IDictionary property");
            Test(new DataWithIDictionaryProperty());
            Console.ReadKey();
        }

        private static void Test(object data)
        {
            try
            {
                var text = Serialize(data);
                var result = Deserialize(text, data.GetType());
            }
            catch (Exception e)
            {
                Console.WriteLine("++++ Failed: " + e.Message + " ++++");
            }
            Console.WriteLine();
        }

        private static object Deserialize(string json, Type type)
        {
            using (var stream = new MemoryStream(Encoding.Unicode.GetBytes(json)))
            {
                var serializer = new DataContractJsonSerializer(type);
                return serializer.ReadObject(stream);
            }

            return null;
        }

        private static string Serialize(object data)
        {
            var stream = new MemoryStream();
            var serializer = new DataContractJsonSerializer(data.GetType());
            serializer.WriteObject(stream, data);

            stream.Position = 0;
            string result;
            using (var reader = new StreamReader(stream))
            {
                result = reader.ReadToEnd();
            }
            Console.WriteLine(result);
            return result;
        }
    }
}
Comment 1 Bryan Crotaz 2013-08-22 12:20:12 UTC
Created attachment 4695 [details]
NUnit test case for json deserialization
Comment 2 Dominique Louis 2013-08-27 08:31:49 UTC
Assigning this to you Kumpera, but if there is someone else who handles Class library issues, let me know who that should be for future reference.
Comment 3 Bryan Crotaz 2013-08-27 08:40:04 UTC
Kumpera - I'm very happy to take a look at this myself if someone can help me get a Windows debug environment up and running.  I've found a couple of other serialization bugs I want to take a look at as well.
Comment 4 Rodrigo Kumpera 2013-08-27 11:43:34 UTC
hi Bryan, we'll be working on sorting out or windows build story. Right now the only way to do it is to use cygwin.
Comment 5 Rodrigo Kumpera 2016-04-14 01:02:17 UTC
This no longer throws with mono 4.4