Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
}
}
}
@greynoO

This comment has been minimized.

Copy link

greynoO commented Jan 15, 2019

I am using the latest version and IRestSerializer, BodyParameter and .UseSerializer(...) do not exist?

@drewfreyling

This comment has been minimized.

Copy link

drewfreyling commented Jan 17, 2019

It should be:

    public class JsonNetSerializer : IRestSerializer
    {
        public string Serialize(object obj) => 
            JsonConvert.SerializeObject(obj);

        public string Serialize(Parameter parameter) => 
            JsonConvert.SerializeObject(parameter.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;
    }
@willchis

This comment has been minimized.

Copy link

willchis commented Mar 27, 2019

@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());

@thompcd

This comment has been minimized.

Copy link

thompcd commented Feb 12, 2020

I had the same experience as @willchis on 106.6.10. Excited to see Newtonsoft make a comeback in 107.

@alexeyzimarev

This comment has been minimized.

@eddypv

This comment has been minimized.

Copy link

eddypv commented Feb 21, 2020

@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();

@stevevg

This comment has been minimized.

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

This comment has been minimized.

Copy link

ferradario commented Aug 6, 2020

This is becoming so annoying!

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

image

@alexeyzimarev

This comment has been minimized.

Copy link
Owner Author

alexeyzimarev commented Aug 6, 2020

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

@alexeyzimarev

This comment has been minimized.

Copy link
Owner Author

alexeyzimarev commented Aug 6, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.