0

In my WPF MVVM application I am getting Data from a restful WCF service in JSON format. I am de-serializing this data using JavaScriptSerializer. But this is not able to handle the null value if any. In this case I have to send some values instead of DBnull value which I do not want to send.

WCF service:

    [OperationContract]
    [WebInvoke
        (
            Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            UriTemplate = "ReportEmployee/All"

        )
    ]
    dcReportEmployee[] GetReportEmployee();

where dcReportEmployee is datacontract

[DataContract]
public class dcReportEmployee
{
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public Int32 Status { get; set; }
}

in WPF Application:

        string sUrl = "LocalHostURL";

        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();

        T[] arrResult = objJsonserialiser.Deserialize<T[]>(objResponseString);

here if objResponseString is containing any null value then its failing. I could have sent some replacement of null values but I do not want that.

Any thought on this ?

dbc
  • 104,963
  • 20
  • 228
  • 340
user2519971
  • 345
  • 3
  • 12
  • 27
  • possible duplicate of [Can JavaScriptSerializer exclude properties with null/default values?](http://stackoverflow.com/questions/1387755/can-javascriptserializer-exclude-properties-with-null-default-values) – flup Nov 27 '13 at 10:36

1 Answers1

0

Use Newtonsoft Library for Serialization.

Priyank
  • 1,353
  • 9
  • 13
  • Why, can you elaborate? Is it the only library that can do the job? Is it clearly much better than the other options? – flup Nov 27 '13 at 11:11
  • The Newtonsoft library has handeled the case of null and complex nested objects which JavaScriptSerializer will not be able to do so. – Priyank Nov 27 '13 at 11:32