none
Extending the WCF deserialiser to deserialise JSON in case-insensitive manner RRS feed

  • Question

  • I have a WCF RESTful service which takes JSON as POST/PUT input and I am trying to figure out if the deserialisation of the stream is possible so that it become case-insensitive. So far I have I have done following:

    1. Put a custom host in Global.Asax

    2. Replace the DataContractSerializerOperationBehaviour with my custom Behaviour 

    I am really not sure how to go about writing the CreateSerialiser to make it case-insensitive. Any help will be tremendously appreciated.

    Wednesday, March 26, 2014 8:25 PM

Answers

  • Hi,

    Please try to apply the MessageParameterattribute to the method arguments and specify the right case in its Name property.

    From MSDN:

    The value of the parameter names in the operation signature are part  of the contract and are case sensitive. Use this attribute when you  need to distinguish between the local parameter name and the metadata  that describes the operation for client applications.

    Also please try to check this similar thread:
    #How to ignore case sensitive properties name in WCF service call:
    http://stackoverflow.com/questions/15574621/how-to-ignore-case-sensitive-properties-name-in-wcf-service-call .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 2, 2014 5:24 AM
    Moderator

All replies

  • You can't. The comparer associated with the dictionary is not part of the data serialization and deserialization.

    You can however easily recreate a dictionary with the same items and the appropriate comparer through the Dictionary(IDictionary dictionary, IEqualityComparer comparer) constructor overload.

    Thursday, March 27, 2014 5:15 AM
  • So how exactly make the WCF use my custom dictionary? And what exactly do I populate my dictionary  with.
    Thursday, March 27, 2014 8:37 PM
  • Hi,

    First of all you must be extra careful with the case sensitive thing because it is case sensitive.

    I will give you an example of how to call a JSON wcf service with arguments and how to get back the result.

    Let's create  a class which will be the post.

    [DataContract]
            public class f_loginClass  //its the post.Place them in the same order you have them in your method.
            {

                private string m_userid;
                private string m_pass;
                private string m_deviceid;

                [DataMember]
                public string userid
                {
                    get { return m_userid; }
                    set { m_userid = value; }
                
                }

                [DataMember]
                public string pass
                {
                    get { return m_pass; }
                    set { m_pass = value; }

                }

                [DataMember]
                public string devid
                {
                    get { return m_deviceid; }
                    set { m_deviceid = value; }

                }

            }

    And create a class which will Keep the result.Careful with the naming convention.

     [DataContract]
            public class f_loginClassResult
            {
                private string m_f_loginResult;

                [DataMember]
                public string f_loginResult
                {
                    get { return m_f_loginResult; }
                    set { m_f_loginResult = value; }
                }
            
            }

    Let's call the WCF service.

    f_loginClass MyJob = new f_loginClass();
                    MyJob.devid ="mydeviceid";
                    MyJob.pass ="mypassword";
                    MyJob.userid = "myiserid";
                    string json = JsonConvert.SerializeObject(MyJob);

    Now I'm about to call the f_login method in my WCF Service. This method takes 3 arguments which their names are devid ,pass,userid. Be careful with the naming it's case sensitive.

    //Calling the WCF Service


                    HttpClient oAuthHttpClient = new HttpClient();
                    oAuthHttpClient.MaxResponseContentBufferSize = 256000; //increase this if you want to pass -get more data
                    oAuthHttpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
                    HttpRequestMessage oAuthRequestMessage = new HttpRequestMessage(HttpMethod.Post, "mywcfservice url"+ "/f_login");
                    oAuthRequestMessage.Content = new StringContent(json);
                    oAuthRequestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                    HttpResponseMessage oAuthResponseMessage = await oAuthHttpClient.SendAsync(oAuthRequestMessage,
    HttpCompletionOption.ResponseContentRead);
                    String responseString = await oAuthResponseMessage.Content.ReadAsStringAsync();
                   f_loginClassResult myresult = new f_loginClassResult();
                    myresult = JsonConvert.DeserializeObject<f_loginClassResult>(responseString);

    if (myresult !=null)

    {

    //your rest of the code...Business logic.

    }

    Hope this will give you a start of what is going on.

    Don't forget the naming convention. That's the most important. Whatever you have as a result (property for eg id) the same you must have in your C# class.

    thank you

    Friday, March 28, 2014 2:55 AM
  • I am not really sure how this fits in with case-insensitive deserialisation of the result. For example, if I pass the JSON like {"UsErId" : "xyz" } then the service will take the "userid" as null.

    I want to make sure that the service picks up the "userid" regardless of the case used.

    Friday, March 28, 2014 5:03 PM
  • Hi,

    Please try to apply the MessageParameterattribute to the method arguments and specify the right case in its Name property.

    From MSDN:

    The value of the parameter names in the operation signature are part  of the contract and are case sensitive. Use this attribute when you  need to distinguish between the local parameter name and the metadata  that describes the operation for client applications.

    Also please try to check this similar thread:
    #How to ignore case sensitive properties name in WCF service call:
    http://stackoverflow.com/questions/15574621/how-to-ignore-case-sensitive-properties-name-in-wcf-service-call .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 2, 2014 5:24 AM
    Moderator