mirror of
https://github.com/Readarr/Readarr.git
synced 2026-04-24 22:35:39 -04:00
added an abstraction layer for json serializer, should work in mono.
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
using System.IO;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
namespace NzbDrone.Common
|
||||
{
|
||||
public interface IJsonSerializer
|
||||
{
|
||||
T Deserialize<T>(string json) where T : class, new();
|
||||
string Serialize(object obj);
|
||||
void Serialize<TModel>(TModel model, Stream outputStream);
|
||||
}
|
||||
|
||||
public class JsonSerializer : IJsonSerializer
|
||||
{
|
||||
private readonly Newtonsoft.Json.JsonSerializer _jsonNetSerializer;
|
||||
|
||||
public JsonSerializer()
|
||||
{
|
||||
var setting = new JsonSerializerSettings
|
||||
{
|
||||
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
|
||||
NullValueHandling = NullValueHandling.Ignore,
|
||||
Formatting = Formatting.Indented,
|
||||
DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate
|
||||
};
|
||||
|
||||
_jsonNetSerializer = new Newtonsoft.Json.JsonSerializer()
|
||||
{
|
||||
DateTimeZoneHandling = setting.DateTimeZoneHandling,
|
||||
NullValueHandling = NullValueHandling.Ignore,
|
||||
Formatting = Formatting.Indented,
|
||||
DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate,
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
public T Deserialize<T>(string json) where T : class, new()
|
||||
{
|
||||
return JsonConvert.DeserializeObject<T>(json);
|
||||
}
|
||||
|
||||
public string Serialize(object obj)
|
||||
{
|
||||
return JsonConvert.SerializeObject(obj);
|
||||
}
|
||||
|
||||
|
||||
public void Serialize<TModel>(TModel model, Stream outputStream)
|
||||
{
|
||||
var jsonTextWriter = new JsonTextWriter(new StreamWriter(outputStream));
|
||||
_jsonNetSerializer.Serialize(jsonTextWriter, model);
|
||||
jsonTextWriter.Flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user