How to get RestSharp to use BSON?

I use RestSharp and use Json.NET for serialization (see here ).

Json.NET supports BSON, and since some of my requests have huge blocks of binary data, I think this will speed up my application significantly. However, as far as I can tell, RestSharp does not have native BSON support.

Using Json.NET is implemented as a custom serializer for RestSharp, and so at first glance it seems that you can use this custom serializer to use BSON. But the Serialize method of the RestSharp.Serializers.ISerializer interface returns a string - which (I suppose) is not suitable for BSON. So, I assume that to make changes to RestSharp more significant changes will be required.

Has anyone figured out how to do this?


Update: I looked at the source of RestSharp and found that the RestRequest.AddBody method, which takes my object and serializes it into the request body, ultimately calls Request.AddParameter (with the serialized object data and the RequestBody parameter RequestBody ).

I thought that I could serialize my object to BSON and then directly call Request.AddParameter - and really can. However, when RestSharp executes RestRequest , it cannot put binary content in the request because there are other built-in assumptions that the contents of the request are UTF-8 encoded.

Thus, it seems that this hack will not work - you will need to make some changes to RestSharp itself, and I'm not the person for this job ...


Update 2: I decided to try using a debugger to find out how much of RestSharp I would have to change to overcome the body encoding problem, so I changed my version of RestSharp to NuGet and included the RestSharp project in my solution. And ... he worked .

It turned out that over the past few months there has been a change in RestSharp, which is not yet in the NuGet version.

So now you can use AddParameter and transfer it to an already-BSON-encoded object, and RestSharp will send it to the server without complaint.

+7
serialization bson restsharp
source share
2 answers

My question updates show that if you have the last RestSharp source, instead:

  request.AddBody(myObject); 

... you can do this instead when you have a payload that will benefit from using BSON:

  using (var memoryStream = new System.IO.MemoryStream()) { using (var bsonWriter = new Newtonsoft.Json.Bson.BsonWriter(memoryStream)) { var serializer = new Newtonsoft.Json.JsonSerializer(); serializer.Serialize(bsonWriter, myObject); var bytes = memoryStream.ToArray(); request.AddParameter("application/bson", bytes, RestSharp.ParameterType.RequestBody); } } 

Note that the first parameter to AddParameter is supposedly the parameter name, but in the case of ParameterType.RequestBody it is actually used as the content type. (Yuk).

Please note that this depends on the change made in RestSharp on April 11, 2013, ewanmellor / ayoung, and this change is not in the current version of NuGet (104.1), therefore, this will only work if you include the current RestSharp source in your project.

+6
source share

Gary answered his question, was incredibly useful for serializing quiet calls. I wanted to answer how to deserialize quiet calls using JSON.NET . I am using RestSharp version 104.4 for Silverlight. My server uses Web API 2.1 with BSON support enabled .

To accept the BSON response, create a BSON Deserializer for RestSharp, for example:

 public class BsonDeserializer : IDeserializer { public string RootElement { get; set; } public string Namespace { get; set; } public string DateFormat { get; set; } public T Deserialize<T>(IRestResponse response) { using (var memoryStream = new MemoryStream(response.RawBytes)) { using (var bsonReader = new BsonReader(memoryStream)) { var serializer = new JsonSerializer(); return serializer.Deserialize<T>(bsonReader); } } } } 

Then make sure your request accepts "application / bson"

 var request = new RestRequest(apiUrl, verb); request.AddHeader("Accept", "application/bson"); 

And add a handler for this type of media

 var client = new RestClient(url); client.AddHandler("application/bson", new BsonDeserializer()); 
+3
source share

All Articles