parent
443e536005
commit
96c59e2b2b
@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace NzbDrone.Common.Serializer
|
||||
{
|
||||
public class UnderscoreStringEnumConverter : JsonConverter
|
||||
{
|
||||
public object UnknownValue { get; set; }
|
||||
|
||||
public UnderscoreStringEnumConverter(object unknownValue)
|
||||
{
|
||||
UnknownValue = unknownValue;
|
||||
}
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return objectType.IsEnum;
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
var enumString = reader.Value.ToString().Replace("_", string.Empty);
|
||||
|
||||
try
|
||||
{
|
||||
return Enum.Parse(objectType, enumString, true);
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (UnknownValue == null)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
|
||||
return UnknownValue;
|
||||
}
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
var enumText = value.ToString();
|
||||
var builder = new StringBuilder(enumText.Length + 4);
|
||||
builder.Append(char.ToLower(enumText[0]));
|
||||
for (int i = 1; i < enumText.Length; i++)
|
||||
{
|
||||
if (char.IsUpper(enumText[i]))
|
||||
{
|
||||
builder.Append('_');
|
||||
}
|
||||
builder.Append(char.ToLower(enumText[i]));
|
||||
}
|
||||
enumText = builder.ToString();
|
||||
|
||||
writer.WriteValue(enumText);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
using FluentAssertions;
|
||||
using Newtonsoft.Json;
|
||||
using NUnit.Framework;
|
||||
using NzbDrone.Core.Download.Clients.DownloadStation;
|
||||
|
||||
namespace NzbDrone.Core.Test.Download.DownloadClientTests.DownloadStationTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class DownloadStationsTaskStatusJsonConverterFixture
|
||||
{
|
||||
[TestCase("captcha_needed", DownloadStationTaskStatus.CaptchaNeeded)]
|
||||
[TestCase("filehosting_waiting", DownloadStationTaskStatus.FilehostingWaiting)]
|
||||
[TestCase("hash_checking", DownloadStationTaskStatus.HashChecking)]
|
||||
[TestCase("error", DownloadStationTaskStatus.Error)]
|
||||
[TestCase("downloading", DownloadStationTaskStatus.Downloading)]
|
||||
public void should_parse_enum_correctly(string value, DownloadStationTaskStatus expected)
|
||||
{
|
||||
var task = "{\"Status\": \"" + value + "\"}";
|
||||
|
||||
var item = JsonConvert.DeserializeObject<DownloadStationTask>(task);
|
||||
|
||||
item.Status.Should().Be(expected);
|
||||
}
|
||||
|
||||
[TestCase("captcha_needed", DownloadStationTaskStatus.CaptchaNeeded)]
|
||||
[TestCase("filehosting_waiting", DownloadStationTaskStatus.FilehostingWaiting)]
|
||||
[TestCase("hash_checking", DownloadStationTaskStatus.HashChecking)]
|
||||
[TestCase("error", DownloadStationTaskStatus.Error)]
|
||||
[TestCase("downloading", DownloadStationTaskStatus.Downloading)]
|
||||
public void should_serialize_enum_correctly(string expected, DownloadStationTaskStatus value)
|
||||
{
|
||||
var task = new DownloadStationTask { Status = value };
|
||||
|
||||
var item = JsonConvert.SerializeObject(task);
|
||||
|
||||
item.Should().Contain(expected);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_return_unknown_if_unknown_enum_value()
|
||||
{
|
||||
var task = "{\"Status\": \"some_unknown_value\"}";
|
||||
|
||||
var item = JsonConvert.DeserializeObject<DownloadStationTask>(task);
|
||||
|
||||
item.Status.Should().Be(DownloadStationTaskStatus.Unknown);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue