Blazor 封装JS组件使用 Json 转换器修正大小写以及名称冲突问题
类型化参数
我们已经看到我们可以将Options参数作为匿名对象传递给构造函数,但我们真的很想使用类型化对象来获得类型安全编程的优点。Options我们首先为仅定义placement字段的类型构造一个新类。
public class Options
{
[JsonPropertyName("placement")]
public Placement Placement { get; set; }
}
public enum Placement
{
[Description("auto")] Auto,
[Description("auto-start")] AutoStart,
[Description("auto-end")] AutoEnd,
[Description("top")] Top,
[Description("top-start")] TopStart,
[Description("top-end")] TopEnd,
[Description("bottom")] Bottom,
[Description("bottom-start")] BottomStart,
[Description("bottom-end")] BottomEnd,
[Description("right")] Right,
[Description("right-start")] RightStart,
[Description("right-end")] RightEnd,
[Description("left")] Left,
[Description("left-start")] LeftStart,
[Description("left-end")] LeftEnd
}
我们为类和枚举添加了许多属性,定义了放置的所有可能选项。JsonPropertyName告诉库System.Text.Json属性Placement名称应该被序列化为placement. 我们添加了Description属性,因为这是最接近实现的地方,我们可以在其中定义不同名称的 JSON 对应项。我们必须定义这些不同的 JSON 对应项的原因是您不能在枚举的选项中使用破折号,例如"top-start". 我们可以选择只使用字符串而不是枚举,但是使用枚举会限制出错的可能性。System.Text.Json不使用Description属性开箱即用,但我们可以定义一个自定义转换器,它可以在序列化为 JSON 时获取属性。在我们制作它时,让我们尝试将泛型考虑在内,以便它可以使用任何带有Description属性的枚举。我们在一个名为EnumDescriptionConverter.
class EnumDescriptionConverter<T> : JsonConverter<T> where T : IComparable, IFormattable, IConvertible
{
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
var description = (DescriptionAttribute)fi.GetCustomAttribute(typeof(DescriptionAttribute), false);
writer.WriteStringValue(description.Description);
}
}
我们定义我们可以使用此类中的任何类型T,只要它实现了 IComparable、IFormattable 和 IConvertible。这几乎将其缩小为枚举。在整个课程中,这T被用作给定类型的占位符。我们还声明该类实现了JsonConverter我们传递 type的接口T。JsonConverter指定我们需要同时实现 aRead和Write方法。我们不会使用该Read方法,所以我们让它抛出一个异常。在该Write方法中,我们的目标是使用writer最后一行中所述的 编写字符串。为了获取可以添加到枚举的任何选项的所有属性,我们Reflection用来获取FieldInfo其中包含所有属性。然后我们从fia中检索DescriptionAttribute并将其转换为 a DescriptionAttribute。如果我们忘记为枚举中的一个选项添加属性,这可能会导致问题,Description但它现在应该可以工作。
然后我们必须告诉我们的类在序列化/反序列化字段Options时应该使用这个转换器。Placement
public class Options
{
[JsonConverter(typeof(EnumDescriptionConverter<Placement>))]
[JsonPropertyName("placement")]
public Placement Placement { get; set; }
}
关联项目
FreeSql QQ群:4336577
BA & Blazor QQ群:795206915
Maui Blazor 中文社区 QQ群:645660665
知识共享许可协议
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名AlexChow(包含链接: https://github.com/densen2014 ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系 。
转载声明
本文来自博客园,作者:周创琳 AlexChow,转载请注明原文链接:https://www.cnblogs.com/densen2014/p/16156891.html
AlexChow
今日头条 | 博客园 | 知乎 | Gitee | GitHub
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 易语言 —— 开山篇