Replace WCF default JSON serialization

前端 未结 2 429
梦谈多话
梦谈多话 2020-11-28 14:46

Is it possible to replace the default JSON serialization of WCF (I\'m currently testing with the webHttp behaviour), and passing application/json a

相关标签:
2条回答
  • 2020-11-28 15:13

    You can use a message formatter to change the serializer used to deal with JSON. The post at https://docs.microsoft.com/en-us/archive/blogs/carlosfigueira/wcf-extensibility-message-formatters shows an example on how to change the default serializer (DataContractJsonSerializer) to another one (JSON.NET).

    0 讨论(0)
  • 2020-11-28 15:13

    Consider creating classes corresponding to your JSON object structure. In that case you don't have to use Dictionary<> like:

    [DataContract]
    public class Customer
    {
        [DataMember(Name="name")]
        public string Name{get;set;}
    
        [DataMember(Name="id")]
        public int ID{get;set;}
    }
    

    This get serialized as:

    {"name": "name-value", "id": "id-value"}
    

    Of course, this is just an alternative to what you already have and may not be applicable.

    0 讨论(0)
提交回复
热议问题