2020-05-01 19:05:28 +01:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Net.Http;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using Newtonsoft.Json.Serialization;
|
|
|
|
|
|
|
|
namespace SpotifyAPI.Web.Http
|
|
|
|
{
|
|
|
|
public class NewtonsoftJSONSerializer : IJSONSerializer
|
|
|
|
{
|
2020-05-02 12:04:26 +01:00
|
|
|
private readonly JsonSerializerSettings _serializerSettings;
|
2020-05-01 19:05:28 +01:00
|
|
|
|
|
|
|
public NewtonsoftJSONSerializer()
|
|
|
|
{
|
|
|
|
_serializerSettings = new JsonSerializerSettings
|
|
|
|
{
|
|
|
|
NullValueHandling = NullValueHandling.Ignore,
|
|
|
|
ContractResolver = new DefaultContractResolver
|
|
|
|
{
|
|
|
|
NamingStrategy = new SnakeCaseNamingStrategy()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public IAPIResponse<T> DeserializeResponse<T>(IResponse response)
|
|
|
|
{
|
|
|
|
Ensure.ArgumentNotNull(response, nameof(response));
|
|
|
|
|
2020-05-03 07:10:41 +01:00
|
|
|
if (response.ContentType?.Equals("application/json", StringComparison.Ordinal) is true || response.ContentType == null)
|
2020-05-01 19:05:28 +01:00
|
|
|
{
|
|
|
|
var body = JsonConvert.DeserializeObject<T>(response.Body as string, _serializerSettings);
|
|
|
|
return new APIResponse<T>(response, body);
|
|
|
|
}
|
|
|
|
return new APIResponse<T>(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SerializeRequest(IRequest request)
|
|
|
|
{
|
|
|
|
Ensure.ArgumentNotNull(request, nameof(request));
|
|
|
|
|
|
|
|
if (request.Body is string || request.Body is Stream || request.Body is HttpContent || request.Body is null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
request.Body = JsonConvert.SerializeObject(request.Body, _serializerSettings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|