3

I do not like how the DataContractSerializer handles my Dictionary deserialization. My methods all return a Stream and I use the JavascriptDeserializer to return the JSON I want, but this does not help me with a Dictionary is one of the POST parameters.

The JavascriptSerializer handles Dictionary's like such:

{"myKey1":"myValue1", "myKey2":"myValue2"}

The DataContractSerializer does this:

[{"Key":"myKey1", "Value":"myValue1"}, {"Key":"myKey2", "Value":"myValue2"}]

The problem with this, is our Android and iPhone apps are puking generating the code natively and our AJAX calls are failing.

Any easy way to do this or a way to get around Microsoft's terrible Dictionary deserialization?

Brandon
  • 10,744
  • 18
  • 64
  • 97
  • You have duplicated your question http://stackoverflow.com/questions/8372076/wcf-post-json-dictionary-without-key-value-text - please remove one of them. Thankyou – tom redfern Dec 05 '11 at 15:34

2 Answers2

2

I had the same problem. I solved it by using a custom Dictionary (actually a wrapper) implementing ISerializable.

[Serializable]
public class CustomDictionary: ISerializable
{
    /// <summary>
    /// Inner object.
    /// </summary>        
    private Dictionary<string, string> innerDictionary;

    public CustomDictionary()
    {
        innerDictionary = new Dictionary<string, string>();
    }

    public CustomDictionary(IDictionary<string, string> dictionary)
    {
        innerDictionary = new Dictionary<string, string>(dictionary);
    }

    public Dictionary<string, string> InnerDictionary
    {
        get { return this.innerDictionary; }
    }

    //Used when deserializing
    protected CustomDictionary(SerializationInfo info, StreamingContext context)
    {
        if (object.ReferenceEquals(info, null)) throw new ArgumentNullException("info");
        innerDictionary = new Dictionary<string, string>();
        foreach (SerializationEntry entry in info)
        {
            innerDictionary.Add(entry.Name, entry.Value as string);
        }
    }

    //Used when serializing
    protected virtual void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        if (!object.ReferenceEquals(info, null))
        {
            foreach (string key in innerDictionary.Keys)
            {
                string value = innerDictionary[key];
                info.AddValue(key, value);
            }
        }
    }

    //Add methods calling InnerDictionary as necessary (ContainsKey, Add, etc...)
}
Romain Meresse
  • 3,044
  • 25
  • 29
0

use the jsonserializer object

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx

here's a blog post detailing a solution: http://pietschsoft.com/post/2008/02/NET-35-JSON-Serialization-using-the-DataContractJsonSerializer.aspx

ttomsen
  • 848
  • 9
  • 15
  • This does not answer my question. I need to know how to DEserialize a dictionary. This is not a returned dictionary, but a dictionary being passed to me by a client application. The DataContract* serializers do not handle Dictionary deserialization the way all of our clients are serializing the dictionary. – Brandon Dec 05 '11 at 14:52
  • http://stackoverflow.com/questions/4199321/how-to-deserialize-a-dictionary-using-datacontractjsonserializer – ttomsen Dec 05 '11 at 15:39