-
-
Save scorpio-angel/1d07ba91985ff19e7b07fce4ad08c8e2 to your computer and use it in GitHub Desktop.
Using Newtonsoft.Json with RestSharp v106.6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment