//------------------------------------------------------------------------------
//
// 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.
//
//------------------------------------------------------------------------------
#nullable enable
namespace LukeHagar.PlexAPI.SDK.Utils
{
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
public class AnyDeserializer : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(Dictionary));
}
public override bool CanWrite => false;
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
throw new NotSupportedException();
}
public override object ReadJson(
JsonReader reader,
Type objectType,
object? existingValue,
JsonSerializer serializer
)
{
if (reader.TokenType == JsonToken.StartObject) {
return ParseTokenIntoDictionary(JToken.Load(reader));
}
throw new JsonSerializationException($"Could not deserialize token into dictionary");
}
private Dictionary ParseTokenIntoDictionary(JToken token)
{
var dict = new Dictionary();
foreach (var child in token.Children())
{
object? val = null;
if (child.Value is JObject)
{
val = ParseTokenIntoDictionary(child.Value);
}
else if (child.Value is JArray)
{
val = ParseTokenIntoList(child.Value);
}
else if (child.Value != null)
{
val = ((JValue)child.Value).Value;
}
dict[child.Name] = val;
}
return dict;
}
private List