使用 System.Text.Json 时,如何处理 Dictionary 中 Key 为自定义类型的问题
在使用 System.Text.Json 进行 JSON 序列化和反序列化操作时,我们会遇到一个问题:如何处理字典中的 Key 为自定义类型的问题。
背景说明
例如,我们有如下代码:
// 定义一个自定义类型
public class CustomType
{
public int Id { get; set; } public string Name { get; set; } // 获取字符串表示的 Key
public string Key => $"{Id}_{Name}";
} // 定义一个 Dictionary<CustomType, string> 类型的对象
Dictionary<CustomType, string> dictionary = new Dictionary<CustomType, string>
{
{ new CustomType { Id = 1, Name = "one" }, "one" },
{ new CustomType { Id = 2, Name = "two" }, "two" }
}; // 序列化字典
string json = JsonSerializer.Serialize(dictionary); // 反序列化字典
Dictionary<CustomType, string> dictionary2 = JsonSerializer.Deserialize<Dictionary<CustomType, string>>(json);
在上述代码中,我们定义了一个自定义类型 CustomType,并使用这个类型作为 Dictionary 的 Key 类型。
接下来,我们使用 JsonSerializer.Serialize 方法将字典序列化为 JSON 字符串,并使用 JsonSerializer.Deserialize 方法将 JSON 字符串反序列化为字典。
但是,在上述代码中,我们会发现,序列化字典时,字典中的 Key 会被序列化为一个 JSON 对象,而不是我们想要的字符串。
同样的,在反序列化 JSON 字符串时,JSON 对象中的 Key 会被反序列化为一个 CustomType 类型的对象,而不是我们想要的字符串。
这时,我们就需要使用一个自定义的 JSON 转换器来解决这个问题。
代码示例
首先,我们定义一个继承自 JsonConverter 的类型 CustomTypeConverter,该类型实现了 Read、Write、ReadAsPropertyName、WriteAsPropertyName 方法:
public class CustomTypeConverter : JsonConverter<CustomType>
{
public override CustomType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
// Deserialize object
return JsonSerializer.Deserialize<CustomType>(ref reader, options);
} public override void Write(Utf8JsonWriter writer, CustomType value, JsonSerializerOptions options)
{
// Serialize object
JsonSerializer.Serialize(writer, value, options);
} public override CustomType ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
// Read key as string
var stringValue = reader.GetString(); // Parse string to CustomType
return ParseCustomType(stringValue);
} public override void WriteAsPropertyName(Utf8JsonWriter writer, CustomType value, JsonSerializerOptions options)
{
// Write key as string
writer.WritePropertyName(value.Key);
} private CustomType ParseCustomType(string value)
{
// Parse string to CustomType
var parts = value.Split("_");
var id = int.Parse(parts[0]);
var name = parts[1]; return new CustomType
{
Id = id,
Name = name
};
}
}
在上述代码中,我们将 CustomType 类型的 Key 属性作为字典的 Key,在序列化操作中,将 Key 属性序列化为字符串,并在反序列化操作中,将字符串反序列化为 Key 属性。
接下来,我们使用这个自定义的 JSON 转换器来序列化和反序列化字典:
// 定义一个自定义类型
public class CustomType
{
public int Id { get; set; } public string Name { get; set; } // 获取字符串表示的 Key
public string Key => $"{Id}_{Name}";
} // 定义一个 Dictionary<CustomType, string> 类型的对象
Dictionary<CustomType, string> dictionary = new Dictionary<CustomType, string>
{
{ new CustomType { Id = 1, Name = "one" }, "one" },
{ new CustomType { Id = 2, Name = "two" }, "two" }
}; // 创建 JsonSerializerOptions 对象
var options = new JsonSerializerOptions(); // 添加自定义的 JSON 转换器
options.
Converters.Add(new CustomTypeConverter()); // 序列化字典
string jsonString = JsonSerializer.Serialize(dictionary, options); // 反序列化 JSON 字符串
var result = JsonSerializer.Deserialize<Dictionary<CustomType, string>>(jsonString, options);
在上述代码中,我们将 CustomType 类型的 Key 属性作为字典的 Key,在序列化操作中,将 Key 属性序列化为字符串,并在反序列化操作中,将字符串反序列化为 Key 属性。
使用建议
在使用 System.Text.Json 进行序列化和反序列化操作时,如果要处理字典中 Key 为自定义类型的问题,可以通过定义一个自定义的 JSON 转换器来解决。
在定义自定义的 JSON 转换器时,需要注意以下几点:
- 类型需要继承自 JsonConverter 类型。
- 类型需要实现 Read、Write、ReadAsPropertyName、WriteAsPropertyName 方法。
- 在 Read 方法中,需要将 JSON 字符串反序列化为 T 类型。
- 在 Write 方法中,需要将 T 类型序列化为 JSON 字符串。
- 在 ReadAsPropertyName 方法中,需要将 JSON 字符串反序列化为字典的 Key 属性。
- 在 WriteAsPropertyName 方法中,需要将字典的 Key 属性序列化为 JSON 字符串。
总结
本文通过一个实例,介绍了如何使用 System.Text.Json 进行序列化和反序列化操作时,处理字典中 Key 为自定义类型的问题。
在定义自定义的 JSON 转换器时,需要注意类型需要继承自 JsonConverter 类型,并实现 Read、Write、ReadAsPropertyName、WriteAsPropertyName 方法。
参考资料
本文采用 Chat OpenAI 辅助注水浇筑而成,如有雷同,完全有可能。
- 本文作者: newbe36524
- 本文链接: https://www.newbe.pro/ChatAI/How-serialize-custom-type-as-dictionary-key-in-system-text-json /
- 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
使用 System.Text.Json 时,如何处理 Dictionary 中 Key 为自定义类型的问题的更多相关文章
- 【译】System.Text.Json 的下一步是什么
.NET 5.0 最近发布了,并带来了许多新特性和性能改进.System.Text.Json 也不例外.我们改进了性能和可靠性,并使熟悉 Newtonsoft.Json 的人更容易采用它.在这篇文章中 ...
- .NET 5的System.Text.Json的JsonDocument类讲解
本文内容来自我写的开源电子书<WoW C#>,现在正在编写中,可以去WOW-Csharp/学习路径总结.md at master · sogeisetsu/WOW-Csharp (gith ...
- 在.Net Core 3.0中尝试新的System.Text.Json API
.NET Core 3.0提供了一个名为System.Text.Json的全新命名空间,它支持reader/writer,文档对象模型(DOM)和序列化程序.在此博客文章中,我将介绍它如何工作以及如何 ...
- .net core中关于System.Text.Json的使用
在.Net Framework的时候序列化经常使用Newtonsoft.Json插件来使用,而在.Net Core中自带了System.Text.Json,号称性能更好,今天抽空就来捣鼓一下. 使用起 ...
- [.Net Core 3.0+/.Net 5] System.Text.Json中时间格式化
简介 .Net Core 3.0开始全新推出了一个名为System.Text.Json的Json解析库,用于序列化和反序列化Json,此库的设计是为了取代Json.Net(Newtonsoft.Jso ...
- 使用.Net6中的System.Text.Json遇到几个常见问题及解决方案
前言 以前.NetCore是不内置JSON库的,所以大家都用Newtonsoft的JSON库,而且也确实挺好用的,不过既然官方出了标准库,那更方便更值得我们多用用,至少不用每次都nuget安装Newt ...
- c# System.Text.Json 精讲
本文内容来自我写的开源电子书<WoW C#>,现在正在编写中,可以去WOW-Csharp/学习路径总结.md at master · sogeisetsu/WOW-Csharp (gith ...
- [译]试用新的System.Text.Json API
译注 可能有的小伙伴已经知道了,在.NET Core 3.0中微软加入了对JSON的内置支持. 一直以来.NET开发者们已经习惯使用Json.NET这个强大的库来处理JSON. 那么.NET为什么要增 ...
- .NET Core 内置的 System.Text.Json 使用注意
System.Text.Json 是 .NET Core 3.0 新引入的高性能 json 解析.序列化.反序列化类库,武功高强,但毕竟初入江湖,炉火还没纯青,使用时需要注意,以下是我们在实现使用中遇 ...
- .NET Core 3.0 System.Text.Json 和 Newtonsoft.Json 行为不一致问题及解决办法
行为不一致 .NET Core 3.0 新出了个内置的 JSON 库, 全名叫做尼古拉斯 System.Text.Json - 性能更高占用内存更少这都不是事... 对我来说, 很多或大或小的项目能少 ...
随机推荐
- Exporter介绍
Exporter是什么 广义上讲所有可以向Prometheus提供监控样本数据的程序都可以被称为一个Exporter.而Exporter的一个实例称为target,如下所示,Prometheus通过轮 ...
- mongodb集群搭建(分片+副本)开启安全认证
关于安全认证得总结: 这个讲述的步骤也是先创建超管用户,关闭服务,然后生成密钥文件,开启安全认证,启动服务 相关概念 先来看一张图: 从图中可以看到有四个组件:mongos.config server ...
- vue this.$router.push query传递对象方法
this.$router.push({ path: '/home', query: { params: JSON.stringify({ name: 'lokka', age: 18 }) } }); ...
- Vue子->父组件传值
父组件引入: Import Test from'' 父页面使用: <Test ref="test" @m1="m2"><Test/> 子 ...
- P3629 [APIO2010] 巡逻 (树的直径)
(这道题考察了求直径的两种方法......) 在原图中,每条边要经过两次,增加1条后,形成了一个环,那么环上的边只需要经过一次了(大量画图分析得),再增加一条又会形成一个环,如果这两个环有重叠,重叠部 ...
- Multi-View Intent Disentangle Graph Networks for Bundle Recommendation解读
简要论述 bundle recommendation 的目的是向user推荐一个整体的bundle package about items.以前的模型捕获了user对item and item关联的偏 ...
- 集合元素的遍历操作,使用迭代器Iterator接口
1.内部的方法:hasNext() 和 next() 推荐的方式: //hasNext():判断是否还有下一个元素while(iterator.hasNext()){ //next():①指针下移 ② ...
- 「MySQL高级篇」explain分析SQL,索引失效&&常见优化场景
大家好,我是melo,一名大三后台练习生 专栏回顾 索引的原理&&设计原则 欢迎关注本专栏:MySQL高级篇 本篇速览 在我们上一篇文章中,讲到了索引的原理&&设计原则 ...
- 一、什么是Kubernetes
一.什么是Kubernetes 它是一个全新的基于容器技术的分布式架构领先方案,确切地说,Kubernetes是谷歌严格保密十几年的秘密武器Borg的一个开源版本.Borg是谷歌内部使用的大规模集群 ...
- 19_Vue如何监测到对象类型数据发生改变的?
数据更新 关于监视 我们之前讲过,我们在data当中配置的属性,最终会挂载在vue实例身上,而data这个配置项,最终也会在vue身上成为一个新的属性 == _data 当我们在页面DOM当中,去使用 ...