mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-20 13:33:34 -07:00
added custom IntConverter to get around the mono bug
http://json.codeplex.com/workitem/24176
This commit is contained in:
parent
904061c2f0
commit
41c5619d1b
15 changed files with 122 additions and 56 deletions
|
@ -8,42 +8,47 @@ namespace NzbDrone.Common.Serializer
|
|||
{
|
||||
public static class Json
|
||||
{
|
||||
private static readonly JsonSerializer JsonNetSerializer;
|
||||
|
||||
private static readonly JsonSerializer Serializer;
|
||||
private static readonly JsonSerializerSettings SerializerSetting;
|
||||
|
||||
static Json()
|
||||
{
|
||||
JsonNetSerializer = new JsonSerializer()
|
||||
{
|
||||
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
|
||||
NullValueHandling = NullValueHandling.Ignore,
|
||||
Formatting = Formatting.Indented,
|
||||
DefaultValueHandling = DefaultValueHandling.Include,
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
};
|
||||
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());
|
||||
|
||||
Serializer = JsonSerializer.Create(SerializerSetting);
|
||||
|
||||
JsonNetSerializer.Converters.Add(new StringEnumConverter { CamelCaseText = true });
|
||||
}
|
||||
|
||||
public static T Deserialize<T>(string json) where T : class, new()
|
||||
public static T Deserialize<T>(string json) where T : new()
|
||||
{
|
||||
return JsonConvert.DeserializeObject<T>(json);
|
||||
return JsonConvert.DeserializeObject<T>(json, SerializerSetting);
|
||||
}
|
||||
|
||||
public static object Deserialize(string json, Type type)
|
||||
{
|
||||
return JsonConvert.DeserializeObject(json, type);
|
||||
return JsonConvert.DeserializeObject(json, type, SerializerSetting);
|
||||
}
|
||||
|
||||
public static string ToJson(this object obj)
|
||||
{
|
||||
return JsonConvert.SerializeObject(obj);
|
||||
return JsonConvert.SerializeObject(obj, SerializerSetting);
|
||||
}
|
||||
|
||||
public static void Serialize<TModel>(TModel model, TextWriter outputStream)
|
||||
{
|
||||
var jsonTextWriter = new JsonTextWriter(outputStream);
|
||||
JsonNetSerializer.Serialize(jsonTextWriter, model);
|
||||
Serializer.Serialize(jsonTextWriter, model);
|
||||
jsonTextWriter.Flush();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue