none
DataContractJsonSerializer help to deserlize Dictionary<string, object[]>

    Question

  • I'm trying to use DataContractJsonSerializer to deserialize a JSON string, that looks like:

     

    {"p1":[123, true], "p2":[456, false]}

     

    The code that I used to deserialize looks something like:

    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Dictionary<string, object[]>));

    ser.ReadObject();

    The output for this is always an empty dictionary with no exceptions.

     

    My question is what should the type parameter be in the constructor? I've noticed that there's another constructor that asks for a knownTypes, could I have perhaps need to put a list of int and bool for what's in the object array? I'm kinda puzzled of what's wrong with this. Please help!

     

    Thanks!

    Tuesday, January 27, 2009 6:14 AM

All replies

  • Dictionaries are serialized in JSONas arrays of KeyValuePair<K, V> - since one can use types other than strings as the key in CLR, it doesn't map directly to a JSON Object.

    To read an arbitrary JSON object into a dictionary using the DataContractJsonSerializer, you can use an ISerializable type:

        public class DeserializingArbitraryJsonObjects  
        {  
            static string json = "{\"p1\": [123, true], \"p2\":[456, false]}";  
     
            [Serializable]  
            public class MyCustomDict : ISerializable  
            {  
                public Dictionary<stringobject[]> dict;  
                public MyCustomDict()  
                {  
                    dict = new Dictionary<stringobject[]>();  
                }  
                protected MyCustomDict(SerializationInfo info, StreamingContext context)  
                {  
                    dict = new Dictionary<stringobject[]>();  
                    foreach (var entry in info)  
                    {  
                        Debug.Assert(entry.ObjectType.IsArray);  
                        object[] array = entry.Value as object[];  
                        dict.Add(entry.Name, array);  
                    }  
                }  
                public void GetObjectData(SerializationInfo info, StreamingContext context)  
                {  
                    foreach (string key in dict.Keys)  
                    {  
                        info.AddValue(key, dict[key]);  
                    }  
                }  
            }  
            static void Main(string[] args)  
            {  
                DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(MyCustomDict));  
                MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));  
                MyCustomDict dict = (MyCustomDict)dcjs.ReadObject(ms);  
                Console.WriteLine(dict.dict.Count);  
            }  
        }  
     
    • Proposed as answer by Carlos Figueira Wednesday, January 28, 2009 8:20 PM
    Tuesday, January 27, 2009 6:53 AM
  • This solution doesn't work for Silverlight, since Silverlight doesn't support the Serializable attribute or the ISerializeable interface. Is there a way to read arbitrary JSON in to an IDictionary<string,object> using DataContractJsonSerializer from Silverlight?

    Wednesday, February 22, 2012 8:32 AM
  • Thank you so much Carlos, it really solved my issue.
    :)
    Wednesday, June 19, 2013 8:07 AM