2013-05-13 04:52:55 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Converters;
|
|
|
|
|
using Newtonsoft.Json.Serialization;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Common.Serializer
|
|
|
|
|
{
|
|
|
|
|
public static class Json
|
|
|
|
|
{
|
|
|
|
|
private static readonly JsonSerializer JsonNetSerializer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static Json()
|
|
|
|
|
{
|
|
|
|
|
JsonNetSerializer = new JsonSerializer()
|
|
|
|
|
{
|
|
|
|
|
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
|
|
|
|
|
NullValueHandling = NullValueHandling.Ignore,
|
|
|
|
|
Formatting = Formatting.Indented,
|
|
|
|
|
DefaultValueHandling = DefaultValueHandling.Include,
|
|
|
|
|
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
JsonNetSerializer.Converters.Add(new StringEnumConverter { CamelCaseText = true });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T Deserialize<T>(string json) where T : class, new()
|
|
|
|
|
{
|
|
|
|
|
return JsonConvert.DeserializeObject<T>(json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static object Deserialize(string json, Type type)
|
|
|
|
|
{
|
|
|
|
|
return JsonConvert.DeserializeObject(json, type);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-29 23:29:51 +02:00
|
|
|
|
public static string ToJson(this object obj)
|
2013-05-13 04:52:55 +02:00
|
|
|
|
{
|
|
|
|
|
return JsonConvert.SerializeObject(obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void Serialize<TModel>(TModel model, TextWriter outputStream)
|
|
|
|
|
{
|
|
|
|
|
var jsonTextWriter = new JsonTextWriter(outputStream);
|
|
|
|
|
JsonNetSerializer.Serialize(jsonTextWriter, model);
|
|
|
|
|
jsonTextWriter.Flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Serialize<TModel>(TModel model, Stream outputStream)
|
|
|
|
|
{
|
|
|
|
|
Serialize(model, new StreamWriter(outputStream));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|