2013-04-17 23:32:06 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2013-04-17 00:24:49 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2013-05-06 01:16:38 +00:00
|
|
|
|
using Newtonsoft.Json.Converters;
|
2013-04-17 00:24:49 +00:00
|
|
|
|
using Newtonsoft.Json.Serialization;
|
|
|
|
|
|
2013-05-12 15:18:17 +00:00
|
|
|
|
namespace NzbDrone.Common.Serializer
|
2013-04-17 00:24:49 +00:00
|
|
|
|
{
|
|
|
|
|
public interface IJsonSerializer
|
|
|
|
|
{
|
|
|
|
|
T Deserialize<T>(string json) where T : class, new();
|
|
|
|
|
string Serialize(object obj);
|
2013-05-06 00:33:43 +00:00
|
|
|
|
void Serialize<TModel>(TModel model, TextWriter textWriter);
|
2013-04-17 00:24:49 +00:00
|
|
|
|
void Serialize<TModel>(TModel model, Stream outputStream);
|
2013-04-17 23:32:06 +00:00
|
|
|
|
object Deserialize(string json, Type type);
|
2013-04-17 00:24:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class JsonSerializer : IJsonSerializer
|
|
|
|
|
{
|
|
|
|
|
private readonly Newtonsoft.Json.JsonSerializer _jsonNetSerializer;
|
|
|
|
|
|
|
|
|
|
public JsonSerializer()
|
|
|
|
|
{
|
2013-05-06 01:16:38 +00:00
|
|
|
|
_jsonNetSerializer = new Newtonsoft.Json.JsonSerializer()
|
2013-04-17 00:24:49 +00:00
|
|
|
|
{
|
|
|
|
|
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
|
2013-05-12 15:18:17 +00:00
|
|
|
|
NullValueHandling = NullValueHandling.Ignore,
|
2013-04-17 00:24:49 +00:00
|
|
|
|
Formatting = Formatting.Indented,
|
2013-05-06 01:16:38 +00:00
|
|
|
|
DefaultValueHandling = DefaultValueHandling.Include,
|
2013-04-17 00:24:49 +00:00
|
|
|
|
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
|
|
|
|
};
|
|
|
|
|
|
2013-05-06 01:16:38 +00:00
|
|
|
|
_jsonNetSerializer.Converters.Add(new StringEnumConverter { CamelCaseText = true });
|
|
|
|
|
}
|
2013-04-17 00:24:49 +00:00
|
|
|
|
|
|
|
|
|
public T Deserialize<T>(string json) where T : class, new()
|
|
|
|
|
{
|
|
|
|
|
return JsonConvert.DeserializeObject<T>(json);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-17 23:32:06 +00:00
|
|
|
|
public object Deserialize(string json, Type type)
|
|
|
|
|
{
|
|
|
|
|
return JsonConvert.DeserializeObject(json, type);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-17 00:24:49 +00:00
|
|
|
|
public string Serialize(object obj)
|
|
|
|
|
{
|
|
|
|
|
return JsonConvert.SerializeObject(obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-05-06 00:33:43 +00:00
|
|
|
|
public void Serialize<TModel>(TModel model, TextWriter outputStream)
|
2013-04-17 00:24:49 +00:00
|
|
|
|
{
|
2013-05-06 00:33:43 +00:00
|
|
|
|
var jsonTextWriter = new JsonTextWriter(outputStream);
|
2013-04-17 00:24:49 +00:00
|
|
|
|
_jsonNetSerializer.Serialize(jsonTextWriter, model);
|
|
|
|
|
jsonTextWriter.Flush();
|
|
|
|
|
}
|
2013-04-17 23:32:06 +00:00
|
|
|
|
|
2013-05-06 00:33:43 +00:00
|
|
|
|
public void Serialize<TModel>(TModel model, Stream outputStream)
|
|
|
|
|
{
|
|
|
|
|
Serialize(model, new StreamWriter(outputStream));
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-17 23:32:06 +00:00
|
|
|
|
|
2013-04-17 00:24:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|