Sorry, but I'm at a loss. This process seems so simplistic to me, but no matter what I do currently I get a new object back with null and zero values. No errors are thrown. I have tried several different processes for converting JSON to a class object, but nothing has worked. Below is the process I'd like to use. Any help as to why this isn't working, would be greatly appreciated.
Please note: What I have to work with uses Hungarian notation. I personally hate it.
//Incoming JSON string to convert:
/*
{"MapPolicySnapshot":{"strMapPolicyID":"189931809","lngLayerTypeID":0,"lngSnapShotID":0,"intZoomLevel":11,"strLayers":",Co unty,HighRisk,Section,CLU,Policy,Draw","strDateChanged":"","strExtent":"-11405656.02395,5258291.144358,-11353411.315124,5282215.934208"}}
*/
[Serializable]
[DataContract(Name = "MapPolicySnapshot")]
public class PolicySnapshot
{
[DataMember(Name = "strMapPolicyID")]
public string strMapPolicyID { get; set; }
[DataMember(Name = "lngLayerTypeID")]
public long lngLayerTypeID { get; set; }
[DataMember(Name = "lngSnapshotID")]
public int lngSnapShotID { get; set; } //Not a typo. Former developer.
[DataMember(Name = "intZoomLevel")]
public int intZoomLevel { get; set; }
[DataMember(Name = "strLayers")]
public string strLayers { get; set; }
[DataMember(Name = "strDateChanged")]
public string strDateChanged { get; set; }
[DataMember(Name = "strExtent")]
public string strExtent { get; set; }
}
public class AController
{
//All other code removed, and no, not the actual controller name
private PolicySnapshot ConvertJSON(string snap)
{
// returns null and zeros
//var snapShot = new JavaScriptSerializer().Deserialize<PolicySnapshot>(snap);
var snapshot = DeserializeJSON<PolicySnapshot>(snap);
return snapshot;
}
private T DeserializeJSON<T>(string json)
{
T obj = Activator.CreateInstance<T>();
var ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
var serializer = new DataContractJsonSerializer(obj.GetType());
obj = (T)serializer.ReadObject(ms);
ms.Close();
return obj;
}
}
When I create a new instance of the PolicySnapshot class with the values from the JSON string, then serialize, I get
{"strMapPolicyID":"189931809","lngLayerTypeID":0,"lngSnapShotID":0,"intZoomLevel":11,"strLayers":",County,HighRisk,Section,CLU,Policy,Draw","strDateChanged":"","strExtent":"-11405656.02395,5258291.144358,-11353411.315124,5282215.934208"}
which is the same data, minus the class name.