RestSharp serialization to JSON, object is not using SerializeAs attribute as expected

前端 未结 5 1735
北荒
北荒 2020-11-30 11:36

I am using RestSharp (version 104.4 via NuGet) to make calls to a Rest Web Service. I have designed a set of objects (POCO) which matches resources exposed

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 11:49

    You could use following method in the Client side. It is essentially using Newtonsoft deserializer instead of built-in RestSharp deserializer. Newtonsoft deserializer respects DataMember Name property or JsonProperty.

        private T Execute(RestRequest request)
        {
            var response = _client.Execute(request);
            if (response.ErrorException != null)
                throw new Exception("Error:" + response.ErrorException);
    
            return (T)JsonConvert.DeserializeObject(response.Content, typeof(T));
        }
    

提交回复
热议问题