You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
2.0 KiB
61 lines
2.0 KiB
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 Serializer;
|
|
private static readonly JsonSerializerSettings SerializerSetting;
|
|
|
|
static Json()
|
|
{
|
|
SerializerSetting = new JsonSerializerSettings
|
|
{
|
|
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
|
|
NullValueHandling = NullValueHandling.Ignore,
|
|
Formatting = Formatting.Indented,
|
|
DefaultValueHandling = DefaultValueHandling.Include,
|
|
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
|
};
|
|
|
|
|
|
SerializerSetting.Converters.Add(new StringEnumConverter { CamelCaseText = true });
|
|
//SerializerSetting.Converters.Add(new IntConverter());
|
|
SerializerSetting.Converters.Add(new VersionConverter());
|
|
|
|
Serializer = JsonSerializer.Create(SerializerSetting);
|
|
|
|
}
|
|
|
|
public static T Deserialize<T>(string json) where T : new()
|
|
{
|
|
return JsonConvert.DeserializeObject<T>(json, SerializerSetting);
|
|
}
|
|
|
|
public static object Deserialize(string json, Type type)
|
|
{
|
|
return JsonConvert.DeserializeObject(json, type, SerializerSetting);
|
|
}
|
|
|
|
public static string ToJson(this object obj)
|
|
{
|
|
return JsonConvert.SerializeObject(obj, SerializerSetting);
|
|
}
|
|
|
|
public static void Serialize<TModel>(TModel model, TextWriter outputStream)
|
|
{
|
|
var jsonTextWriter = new JsonTextWriter(outputStream);
|
|
Serializer.Serialize(jsonTextWriter, model);
|
|
jsonTextWriter.Flush();
|
|
}
|
|
|
|
public static void Serialize<TModel>(TModel model, Stream outputStream)
|
|
{
|
|
Serialize(model, new StreamWriter(outputStream));
|
|
}
|
|
}
|
|
} |