using System; using System.Reflection; using System.Text.Json; namespace Discord.Serialization.Json { internal class JsonPropertyMap : PropertyMap, IJsonPropertyMap { private readonly IJsonPropertyConverter _converter; private readonly Func _getFunc; private readonly Action _setFunc; public JsonPropertyMap(PropertyInfo propInfo, IJsonPropertyConverter converter) : base(propInfo) { _converter = converter; _getFunc = propInfo.GetMethod.CreateDelegate(typeof(Func)) as Func; _setFunc = propInfo.SetMethod.CreateDelegate(typeof(Action)) as Action; } public void Write(TModel model, ref JsonWriter writer) { var value = _getFunc(model); if (value == null && ExcludeNull) return; _converter.Write(this, ref writer, value, true); } public void Read(TModel model, ref JsonReader reader) { var value = _converter.Read(this, ref reader, true); _setFunc(model, value); } } }