-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
JsonRequestBodySerializer.cs
37 lines (33 loc) · 1.13 KB
/
JsonRequestBodySerializer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using Newtonsoft.Json;
using System.Net.Http;
using System.Net.Http.Headers;
namespace RestEase
{
/// <summary>
/// Default IRequestBodySerializer, using Json.NET
/// </summary>
public class JsonRequestBodySerializer : RequestBodySerializer
{
/// <summary>
/// Gets or sets the serializer settings to pass to JsonConvert.SerializeObject
/// </summary>
public JsonSerializerSettings? JsonSerializerSettings { get; set; }
/// <inheritdoc/>
public override HttpContent? SerializeBody<T>(T body, RequestBodySerializerInfo info)
{
if (body == null)
return null;
var content = new StringContent(JsonConvert.SerializeObject(body, this.JsonSerializerSettings));
const string contentType = "application/json";
if (content.Headers.ContentType == null)
{
content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
}
else
{
content.Headers.ContentType.MediaType = contentType;
}
return content;
}
}
}