title: Convert object to json using RestSharp only
body: <p>I've created a small class like this one.</p>
  
  <pre><code>public class Device
      {
          public Device()
          {
          }
  
          [DeserializeAs(Name = "u_friendly_name")]
          //[SerializeAs(Name = "u_friendly_name")]
          public string Name { get; set; }
      }
  }
  </code></pre>
  
  <p>In order to map "Name" to "u_friendly_name" (as it should be in json) I've added a DeserializeAs declaration. My first question here is.. do I need both DeserializeAs and SerializeAs if I want to POST/GET json or would DeserializeAs suffice?</p>
  
  <p>Doing a GET like this works perfectly. It automatically manages to get the json and cast it to a device as required. However, doing a POST wasn't as simple...</p>
  
  <pre><code>public Device GetDevice(string guid)
  {
      var request = new RestRequest("api/now/table/x_device", Method.GET);
      request.RootElement = "result";
  
      var devices = client.Execute&lt;List&lt;Device&gt;&gt;(request);
      return devices.Data.FirstOrDefault(d =&gt; d.Guid == guid);
  }
  </code></pre>
  
  <p>I'm trying to do a POST like this.</p>
  
  <pre><code>        public void RegisterDevice()
          {
              Device device = new Device();
              device.Name = Environment.MachineName;
  
              var request = new RestRequest("api/now/table/x_device", Method.POST);
              request.RequestFormat = DataFormat.Json;
              request.AddBody(device); // HERE
  
              Console.WriteLine(request.Parameters[0]);
  
              var response = client.Execute(request);
          }
  </code></pre>
  
  <p>The problem is that the object isn't translated into json. Instead of having "u_friendly_name" in the json output I see "Name". I know this can be solved using Json.Net such as:</p>
  
  <pre><code>string json = JsonConvert.SerializeObject(device);
  </code></pre>
  
  <p>But that would also require me to add another dependency to my project and add the JsonProperty declaration to my class. I have been able to get POST working with Json.NET, but not GET and vise versa.</p>
  
  <p>Anyone who can point me in the right direction of how to serialize my class using (preferably) only RestSharp?</p>
  
  <p><strong>Edit:</strong> After reading a lot of posts on stackoverflow, I get the impression that RestSharp just isn't suitable for deserialization. I found the following link that uses a custom json serializer and while POST works great, I'm not sure what I'm doing wrong with GET: <a href="http://bytefish.de/blog/restsharp_custom_json_serializer/" rel="nofollow noreferrer">http://bytefish.de/blog/restsharp_custom_json_serializer/</a></p>
  
  <pre><code>public Device GetDevice(string guid)
  {
      var request = new RestRequest("api/now/table/x_device", Method.GET);
      request.RequestFormat = DataFormat.Json;
      request.JsonSerializer = NewtonsoftJsonSerializer.Default;
      request.RootElement = "result";
  
      var devices = client.Execute&lt;List&lt;Device&gt;&gt;(request);
      return devices.Data.FirstOrDefault(d =&gt; d.Guid == guid);
  }
  </code></pre>
  
  <p>The only thing that works is setting both:</p>
  
  <pre><code>[JsonProperty(PropertyName = "u_friendly_name")]
  [DeserializeAs(Name = "u_friendly_name")]
  </code></pre>
  
  <p>But that shouldn't be necessary I hope?</p>
  
  <p><strong>Edit2:</strong> I have manged to get POST to work by adding the "AddHandlers" to the RestSharp client as mentioned in the blog post above. I have also changed my class and added a "Devices" definition.</p>
  
  <pre><code>public class Devices
  {
      [JsonProperty(PropertyName = "result")]
      public List&lt;Device&gt; devices { get; set; }
  }
  
  public class Device
      {
          public Device()
          {
          }
  
          [DeserializeAs(Name = "u_friendly_name")]
          //[SerializeAs(Name = "u_friendly_name")]
          public string Name { get; set; }
      }
  }
  </code></pre>
  
  <p>Now my function looks like this and works as intended:</p>
  
  <pre><code>public Device GetDevice(string guid)
  {
      var request = new RestRequest("api/now/table/x_uia_sdm_device", Method.GET);
      request.RequestFormat = DataFormat.Json;
      request.JsonSerializer = NewtonsoftJsonSerializer.Default;
  
      IRestResponse response = client.Execute(request);
      var deviceList = JsonConvert.DeserializeObject&lt;Devices&gt;(response.Content);
      return deviceList.devices.FirstOrDefault(d =&gt; d.Guid == guid);
  }
  </code></pre>
  
  <p>Json:</p>
  
  <pre><code>{
    "result": [
      {
        "u_guid": "12345678",
        "u_friendly_name": "DeviceA",
        "sys_mod_count": "1",
        "sys_updated_on": "2017-03-09 11:58:50",
        "sys_tags": "",
        "u_config_override": "log_level_local = 5",
        "u_running_version": "1.0.0",
        "sys_id": "abcdefg",
        "sys_created_on": "2017-03-07 12:25:06",
        "u_reference_record": "",
        "u_status": "Provisioned",
        "sys_created_by": "foo@bar.no"
      },
      {
        "u_guid": "34567890",
        "u_friendly_name": "DeviceB",
        "sys_mod_count": "0",
        "sys_updated_on": "2017-03-08 13:23:06",
        "sys_tags": "",
        "u_config_override": "",
        "u_running_version": "",
        "sys_id": "abcdefg",
        "u_application": "",
        "u_reference_record": "",
        "u_status": "",
        "sys_created_by": "foo@bar.no"
      }
    ]
  }
  </code></pre>
  
  <p>Even though it now works my initial question still stands. If possible I would love to remove Json.Net and only use RestSharp.</p>