1
0
Fork 0
mirror of https://github.com/Radarr/Radarr synced 2024-12-27 10:19:41 +00:00
Radarr/NzbDrone.Common/Serializer/IntConverter.cs

39 lines
1.1 KiB
C#
Raw Normal View History

using System;
using Newtonsoft.Json;
namespace NzbDrone.Common.Serializer
{
public class IntConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value == null)
{
writer.WriteNull();
}
else
{
writer.WriteValue(value);
}
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (!CanConvert(objectType))
{
throw new JsonSerializationException("Can't convert type " + existingValue.GetType().FullName + " to number");
}
if (objectType == typeof(Int64))
{
return Convert.ToInt64(reader.Value);
}
return Convert.ToInt32(reader.Value);
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Int32) || objectType == typeof(Int64) || objectType == typeof(int);
}
}
}