Skip to content

Instantly share code, notes, and snippets.

@alexeyzimarev
Created December 30, 2018 21:08
Show Gist options
  • Save alexeyzimarev/c00b79c11c8cce6f6208454f7933ad24 to your computer and use it in GitHub Desktop.
Save alexeyzimarev/c00b79c11c8cce6f6208454f7933ad24 to your computer and use it in GitHub Desktop.
Using Newtonsoft.Json with RestSharp v106.6
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;
}
}
}
@stevevg
Copy link

stevevg commented Aug 6, 2020

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
Copy link

This is becoming so annoying!

Please update documentation that still reports full support for newtonsoft...

image

@alexeyzimarev
Copy link
Author

@ferradario the documentation is correct. Take your annoyance somewhere else.

@alexeyzimarev
Copy link
Author

@stevevg that is correct, SimpleJson uses the SerializeAs and DeserializeAs attributes.

@centu81
Copy link

centu81 commented Nov 16, 2020

So what is the final usage of Newtonsoft.Json in RestSharp?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment