-
-
Save alexeyzimarev/c00b79c11c8cce6f6208454f7933ad24 to your computer and use it in GitHub Desktop.
using Newtonsoft.Json; | |
using RestSharp; | |
using RestSharp.Serialization; | |
namespace JsonNetRestSharp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var client = new RestClient("https://my.api.com") | |
.UseSerializer(new JsonNetSerializer()); | |
} | |
public class JsonNetSerializer : IRestSerializer | |
{ | |
public string Serialize(object obj) => | |
JsonConvert.SerializeObject(obj); | |
public string Serialize(BodyParameter bodyParameter) => | |
JsonConvert.SerializeObject(bodyParameter.Value); | |
public T Deserialize<T>(IRestResponse response) => | |
JsonConvert.DeserializeObject<T>(response.Content); | |
public string[] SupportedContentTypes { get; } = | |
{ | |
"application/json", "text/json", "text/x-json", "text/javascript", "*+json" | |
}; | |
public string ContentType { get; set; } = "application/json"; | |
public DataFormat DataFormat { get; } = DataFormat.Json; | |
} | |
} | |
} |
@greynoO I updated to 106.6.9 in order to get this to work. The suggested method .UseSerializer(new JsonNetSerializer());
is also marked as deprecated so I used this overload instead:
.UseSerializer(() => new JsonNetSerializer());
I had the same experience as @willchis on 106.6.10. Excited to see Newtonsoft make a comeback in 107.
@greynoO I updated to 106.6.9 in order to get this to work. The suggested method
.UseSerializer(new JsonNetSerializer());
is also marked as deprecated so I used this overload instead:
.UseSerializer(() => new JsonNetSerializer());
@willchis it works like this
var request = new RestRequest();
request.JsonSerializer = new JsonNetSerializer();
Thanks! Using Newtonsoft.Json
, deserization works even with DataMember
annotated properties, whereas built-in deserializer seems to ignore those properties, always returning default value for associated type.
@ferradario the documentation is correct. Take your annoyance somewhere else.
@stevevg that is correct, SimpleJson uses the SerializeAs
and DeserializeAs
attributes.
So what is the final usage of Newtonsoft.Json in RestSharp?
It should be: