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.
45 lines
1.6 KiB
45 lines
1.6 KiB
4 years ago
|
using Newtonsoft.Json;
|
||
|
using System;
|
||
|
|
||
|
namespace Discord.Net.Converters
|
||
|
{
|
||
|
internal class PermissionTargetConverter : JsonConverter
|
||
|
{
|
||
|
public static readonly PermissionTargetConverter Instance = new PermissionTargetConverter();
|
||
|
|
||
|
public override bool CanConvert(Type objectType) => true;
|
||
|
public override bool CanRead => true;
|
||
|
public override bool CanWrite => true;
|
||
|
|
||
|
/// <exception cref="JsonSerializationException">Unknown permission target.</exception>
|
||
|
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||
|
{
|
||
|
switch ((string)reader.Value)
|
||
|
{
|
||
|
case "member":
|
||
|
return PermissionTarget.User;
|
||
|
case "role":
|
||
|
return PermissionTarget.Role;
|
||
|
default:
|
||
|
throw new JsonSerializationException("Unknown permission target.");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <exception cref="JsonSerializationException">Invalid permission target.</exception>
|
||
|
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||
|
{
|
||
|
switch ((PermissionTarget)value)
|
||
|
{
|
||
|
case PermissionTarget.User:
|
||
|
writer.WriteValue("member");
|
||
|
break;
|
||
|
case PermissionTarget.Role:
|
||
|
writer.WriteValue("role");
|
||
|
break;
|
||
|
default:
|
||
|
throw new JsonSerializationException("Invalid permission target.");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|