2013-04-18 01:32:06 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2013-04-17 02:24:49 +02:00
|
|
|
|
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);
|
2013-04-18 01:32:06 +02:00
|
|
|
|
object Deserialize(string json, Type type);
|
2013-04-17 02:24:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
2013-04-25 06:27:49 +02:00
|
|
|
|
NullValueHandling = setting.NullValueHandling,
|
|
|
|
|
Formatting = setting.Formatting,
|
|
|
|
|
DefaultValueHandling = setting.DefaultValueHandling,
|
2013-04-17 02:24:49 +02:00
|
|
|
|
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public T Deserialize<T>(string json) where T : class, new()
|
|
|
|
|
{
|
|
|
|
|
return JsonConvert.DeserializeObject<T>(json);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-18 01:32:06 +02:00
|
|
|
|
public object Deserialize(string json, Type type)
|
|
|
|
|
{
|
|
|
|
|
return JsonConvert.DeserializeObject(json, type);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-17 02:24:49 +02:00
|
|
|
|
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();
|
|
|
|
|
}
|
2013-04-18 01:32:06 +02:00
|
|
|
|
|
|
|
|
|
|
2013-04-17 02:24:49 +02:00
|
|
|
|
}
|
|
|
|
|
}
|