Reputation: 345
I am using below generic method to deserialize my json response in a wcf application. However it is taking long time to deserialize approximately 5 MB of data.
Program execution always gets stuck at below line :
T[] arrResult = objJsonserialiser.Deserialize<T[]>(objResponseString);
public ObservableCollection<T> InvokeGet<T>(string sUrl )
{
System.Net.WebRequest request = System.Net.HttpWebRequest.Create(sUrl);
request.Method = "GET";
request.UseDefaultCredentials = true;
request.ContentLength = 0;
System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse;
Stream objResponseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(objResponseStream);
string objResponseString = reader.ReadToEnd();
response.Close();
JavaScriptSerializer objJsonserialiser = new JavaScriptSerializer();
objJsonserialiser.MaxJsonLength = 999999999;
T[] arrResult = objJsonserialiser.Deserialize<T[]>(objResponseString);
return new ObservableCollection<T>(arrResult);
}
Can I optimize this by any other means ?
Upvotes: 0
Views: 1042
Reputation: 345
I used Newtonsoft.Json for de-serializing the json data and got the performance improvement of approx 80 %.
T[] arrResult = JsonConvert.DeserializeObject(objResponseString);
Upvotes: 1
Reputation: 1577
while searching on net I found that javascript serialiser takes along time to parse Json. I would prefer you to use Json.net for Json Parsing as its quite faster comparatively.
However try this. it will return the result in the form of Key Value pairs and check if its working.
var test = objJsonserialiser.Deserialize<IDictionary<string, object>>(objResponseString);
Upvotes: 0