lucidiot/HabitSharp
lucidiot
/
HabitSharp
Archived
1
0
Fork 0

Use Newtonsoft.Json as serializer

This commit is contained in:
Lucidiot 2018-10-01 03:17:56 +02:00
parent 582b474747
commit 920187e876
No known key found for this signature in database
GPG Key ID: AE3F7205692FA205
3 changed files with 101 additions and 39 deletions

View File

@ -5,6 +5,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="RestSharp" Version="106.3.1" />
</ItemGroup>

View File

@ -1,39 +1,48 @@
using System;
using System.Collections.Generic;
using RestSharp;
using HabitSharp.Managers;
namespace HabitSharp
{
public class HabiticaApi
{
const string BaseURL = "https://habitica.com/api/v3";
private readonly RestClient Client;
public readonly TagManager Tags;
public HabiticaApi(string UserID, string ApiKey) : this(Guid.Parse(UserID), Guid.Parse(ApiKey)) { }
public HabiticaApi(Guid UserID, Guid ApiKey)
{
this.Client = new RestClient(BaseURL);
this.Client.Authenticator = new HabiticaAuthenticator(UserID, ApiKey);
this.Tags = new TagManager(this);
}
public T Execute<T>(RestRequest request) where T : new() {
var response = Client.Execute<T>(request);
if (response.ErrorException != null) throw new ApplicationException(
"An error occured while running a Habitica API request. See inner exception for more info",
response.ErrorException);
return response.Data;
}
public Result<Dictionary<string, string>> GetModelPaths(string ModelName) {
var req = new RestRequest("models/{model}/paths");
req.AddUrlSegment("model", ModelName);
return this.Execute<Result<Dictionary<string, string>>>(req);
}
}
}
using System;
using System.Collections.Generic;
using RestSharp;
using HabitSharp.Managers;
using HabitSharp.Serialization;
namespace HabitSharp
{
public class HabiticaApi
{
const string BaseURL = "https://habitica.com/api/v3";
private readonly RestClient Client;
public readonly TagManager Tags;
public HabiticaApi(string UserID, string ApiKey) : this(Guid.Parse(UserID), Guid.Parse(ApiKey)) { }
public HabiticaApi(Guid UserID, Guid ApiKey)
{
this.Client = new RestClient(BaseURL);
this.Client.Authenticator = new HabiticaAuthenticator(UserID, ApiKey);
// Override JSON serialization
this.Client.AddHandler("application/json", NewtonsoftJsonSerializer.Default);
this.Client.AddHandler("text/json", NewtonsoftJsonSerializer.Default);
this.Client.AddHandler("text/x-json", NewtonsoftJsonSerializer.Default);
this.Client.AddHandler("text/javascript", NewtonsoftJsonSerializer.Default);
this.Client.AddHandler("*+json", NewtonsoftJsonSerializer.Default);
this.Tags = new TagManager(this);
}
public T Execute<T>(RestRequest request) where T : new() {
var response = Client.Execute<T>(request);
if (response.ErrorException != null) throw new ApplicationException(
"An error occured while running a Habitica API request. See inner exception for more info",
response.ErrorException);
return response.Data;
}
public Result<Dictionary<string, string>> GetModelPaths(string ModelName) {
var req = new RestRequest("models/{model}/paths");
req.AddUrlSegment("model", ModelName);
return this.Execute<Result<Dictionary<string, string>>>(req);
}
}
}

View File

@ -0,0 +1,52 @@
using System.IO;
using Newtonsoft.Json;
using RestSharp.Serializers;
using RestSharp.Deserializers;
namespace HabitSharp.Serialization {
public class NewtonsoftJsonSerializer : ISerializer, IDeserializer
{
private Newtonsoft.Json.JsonSerializer serializer;
public NewtonsoftJsonSerializer(Newtonsoft.Json.JsonSerializer serializer) => this.serializer = serializer;
public string ContentType {
get => "application/json";
set { }
}
public string DateFormat { get; set; }
public string Namespace { get; set; }
public string RootElement { get; set; }
public string Serialize(object obj)
{
using (var stringWriter = new StringWriter())
{
using (var jsonTextWriter = new JsonTextWriter(stringWriter))
{
serializer.Serialize(jsonTextWriter, obj);
return stringWriter.ToString();
}
}
}
public T Deserialize<T>(RestSharp.IRestResponse response)
{
using (var stringReader = new StringReader(response.Content))
{
using (var jsonTextReader = new JsonTextReader(stringReader))
{
return serializer.Deserialize<T>(jsonTextReader);
}
}
}
public static NewtonsoftJsonSerializer Default =>
new NewtonsoftJsonSerializer(new Newtonsoft.Json.JsonSerializer() {
NullValueHandling = NullValueHandling.Ignore,
});
}
}