mirror of
https://github.com/LukeHagar/plexcsharp.git
synced 2025-12-06 20:47:49 +00:00
63 lines
2.0 KiB
C#
63 lines
2.0 KiB
C#
//------------------------------------------------------------------------------
|
|
// <auto-generated>
|
|
// This code was generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
|
|
//
|
|
// Changes to this file may cause incorrect behavior and will be lost when
|
|
// the code is regenerated.
|
|
// </auto-generated>
|
|
//------------------------------------------------------------------------------
|
|
#nullable enable
|
|
using System;
|
|
using Newtonsoft.Json;
|
|
namespace LukeHagar.PlexAPI.SDK.Utils
|
|
{
|
|
internal class OpenEnumConverter : JsonConverter
|
|
{
|
|
public override bool CanConvert(System.Type objectType)
|
|
{
|
|
return objectType.GetMethod("Of") != null && objectType.GetMethod("ToString") != null;
|
|
}
|
|
|
|
public override object? ReadJson(
|
|
JsonReader reader,
|
|
System.Type objectType,
|
|
object? existingValue,
|
|
JsonSerializer serializer
|
|
)
|
|
{
|
|
if (reader.Value == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var method = objectType.GetMethod("Of");
|
|
if (method == null)
|
|
{
|
|
throw new Exception($"Unable to find Of method on {objectType}");
|
|
}
|
|
try {
|
|
return method.Invoke(null, new[] { reader.Value });
|
|
} catch(System.Reflection.TargetInvocationException e) {
|
|
throw new Newtonsoft.Json.JsonSerializationException("Unable to convert value to open enum", e);
|
|
}
|
|
}
|
|
|
|
public override void WriteJson(JsonWriter writer, object? obj, JsonSerializer serializer)
|
|
{
|
|
if (obj == null)
|
|
{
|
|
writer.WriteValue("null");
|
|
return;
|
|
}
|
|
|
|
var valueProp = obj.GetType().GetProperty("Value");
|
|
if (valueProp == null)
|
|
{
|
|
throw new Exception($"{obj.GetType()} does not have a Value property");
|
|
}
|
|
|
|
writer.WriteValue(valueProp.GetValue(obj));
|
|
}
|
|
}
|
|
}
|