Search code examples
c#jsonwcfjavascriptserializer

JavaScriptSerializer is taking long time to Deserialize WCF json Data


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 ?


Solution

  • I used Newtonsoft.Json for de-serializing the json data and got the performance improvement of approx 80 %.

    T[] arrResult = JsonConvert.DeserializeObject(objResponseString);