public object GetDeserializedObject(object obj, Type targetType) { if (obj is ListDictionaryArray) { var array = (ListDictionaryArray)obj; var dict = array.SelectMany(pair => pair).ToDictionary(pair => pair.Key, pair => pair.Value); var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(dict); return(DataContractJsonSerializerHelper.GetObject <T>(json, new DataContractJsonSerializerSettings { UseSimpleDictionaryFormat = true })); } return(obj); }
public static void Test() { var a1 = new A() { s1 = "A" }; var b1 = new B() { s2 = "B" }; DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(IObject)); var jsona1 = DataContractJsonSerializerHelper.GetJson(a1, serializer); var jsonb1 = DataContractJsonSerializerHelper.GetJson(b1, serializer); Debug.WriteLine(jsona1); Debug.WriteLine(jsonb1); var newa1 = DataContractJsonSerializerHelper.GetObject <IObject>(jsona1, serializer); Debug.Assert(newa1.GetType() == a1.GetType()); // No assert }