Question

I'm trying to improve the default RestSharp serialization by using Json.net library. In order to customize the serialization you have to implement ISerializer interface:

public class LowerCaseSerializer : ISerializer{
    public LowerCaseSerializer(){
        ContentType = "application/json";
    }

    public string Serialize(object obj){
         var settings = new JsonSerializerSettings{
            ContractResolver = new LowerCaseResolver()
        };
        return JsonConvert.SerializeObject(obj, Formatting.None, settings);
    }

    string ISerializer.RootElement { get; set; }
    string ISerializer.Namespace { get; set; }
    string ISerializer.DateFormat { get; set; }
    public string ContentType { get; set; }
}

As you see I'm also extending the ContractResolver. This is the actual code that does the lowercasing:

public class LowerCaseResolver : DefaultContractResolver{
    protected override string ResolvePropertyName(string propertyName){
        return propertyName.ToLower();
    }
}

Once all this is setup I can use it with RestSharp:

var request = new RestRequest(url, method);
if (ShouldAddBody(method)){
    request.JsonSerializer = new LowerCaseSerializer();
    request.AddObject(body);
}
var response = client.Execute<T>(request);

Everything works, except the properties are not in lower case. When debugging the debuger goes into the Constructor of the serializers, but it's method is never called. When I tried exactly the same for deserializations (IDeserialize interface, which attaches to the client) the method for lower casing was called for each property.

What I have also tried:

request.RequestFormat = DataFormat.Json;  // no change
// this correctly lower cases the properties
var json = new LowerCaseSerializer().Serialize(body);
// wrong request sent to the server, it tells me that some property is missing
request.AddBody(json);
// the exact same thing with this
request.AddParameter("application/json", json, ParameterType.RequestBody);

The thing I noticed with the last two: if I have lower case properties and let RestSharp serializes then the request has 5 parameters (one for each property). If I add it via upper two methods it has only one property and that's the whole json.

I check the RestSharp issues to no avail. Any suggestions?

Update:

This very strange:

  • forked the RestSharp, installed Json.net, works fine
  • copied RestRequest class from RestSharp fork, pasted to my application, used it, works fine
  • started new project, installed RestSharp and Json.net via Package manager, works fine

Then removed all packages from my main application, redownloaded, doesn't work. Kinda giving up on this.

Update 2: Debugging through the forked version I noticed this: The RequestFormat has to be Dataformat.Json or it will use Xml serializer. But that doesn't fix the problem. I tried setting both (there are only two) serializers to null:

request.JsonSerializer = null;
request.XmlSerializer = null;

In the new project I did this causes NullReferrenceException as expected. But in the old one nothing happens. I tried renaming the variables, making another variable of the same type, but nothing fixes is. It just seems that in the project I have the RestRequest class is somehow bugged.

I also added a new project to the solution. And even there the code works. So it's just one project that has the problem.

Was it helpful?

Solution

Since you can't reproduce it in a new project, there must be something different going on in this particular project, that's causing the issues, you're describing.

A couple of things you could try (in no particular order):

  • Check that you're using the exact same version of the library in both projects (the one that works and the one that doesn't): package version and target platform (net4, net35...).

  • Delete the packages folder of your non-working project so that NuGet will be forced to re-download all the packages.

  • Lookup the exact path to the referenced library in Visual Studio Properties window when you have RestSharp from References node selected. Do a binary compare between the libraries referenced by the working and the non-working project.

  • Unfortunately there's no symbol package for RestSharp on SymbolSource, so you can't directly debug RestSharp in your non-working project. You could use Reflector.NET VSPro if you have the license or haven't used the trial before.

  • Move parts of your non-working project to the working one until it stops working.

EDIT:

Looking at the source code of RestRequest, AddObject doesn't seem to use the JsonSerializer you are setting. Have you tried using AddBody instead?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top