using Newtonsoft.Json;
using System;

namespace Pilz.Configuration;

public class SettingsValueOptionJsonConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return true;
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (existingValue is ISettingsValueOptionValueAccessor option)
            option.ValueRaw = reader.Value;
        return existingValue;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        if (value is ISettingsValueOptionValueAccessor option)
            writer.WriteValue(option.ValueRaw);
        else
            writer.WriteNull();
    }
}
