gusadolfo
gusadolfo

Reputation: 65

Restsharp serialize complex object for json

I'm having trouble serializing an complex object with Restsharp on Asp.Net MVC 4. When I send it the object doesn't arrive the full object it only arrives with the string or int or long, the Lists or even the IList won't arrive on the object

This is are my objects:

public class Project
   {       
       public long Id { get; set; }
       public string NumPol { get; set; }
       public string Name { get; set; }
       public string Status { get; set; }
       public System.DateTime CreationDate { get; set; }      
       public System.DateTime RenewalDate { get; set; }       
       public System.DateTime ExpirationDate { get; set; }        
       public long Notification { get; set; }                
       public decimal TotalSum { get; set; }        
       public int NoRenewal { get; set; }        
       public int Cancellation { get; set; }        
       public IList<Coin> Coinss { get; set; }

   }

public class Moneda
   {
       public int Id { get; set; }
       public string Name { get; set; }
   }

and the Restsharp:

RestClient client = new RestClient("http://localhost:9212/");
       RestRequest request = new RestRequest("Pol/CreatePol", Method.PUT);            
       request.RequestFormat = DataFormat.Json;
       request.AddObject(project);
       IRestResponse<ProjectoPol> response = client.Execute<ProjectoPol>(request);

any suggestions on how to fix this??

Upvotes: 0

Views: 3764

Answers (1)

Eric Pohl
Eric Pohl

Reputation: 2344

Based on the fact that you're setting

request.RequestFormat = DataFormat.Json;

I'm assuming you want the project object as JSON in the body of the request. To do this, you would use

request.AddBody(project);

rather than

request.AddObject(project);

I've never used AddObject(), but if I understand the source comments correctly, it's adding the properties of the object to your request as form parameters.

Upvotes: 1

Related Questions