System.Text.Json API 是否有类似 IContractResolver 的东西

编程入门 行业动态 更新时间:2024-10-13 18:25:51
本文介绍了System.Text.Json API 是否有类似 IContractResolver 的东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在新的 System.Text.Json 中;命名空间是否有类似 IContractResolver 的东西,我正在尝试将我的项目从 Newtonsoft 迁移.

In the new System.Text.Json; namespace is there something like IContractResolver i am trying to migrate my project away from Newtonsoft.

这是我想转移的课程之一:

This is one of the classes i am trying to move:

public class SelectiveSerializer : DefaultContractResolver { private readonly string[] fields; public SelectiveSerializer(string fields) { var fieldColl = fields.Split(','); this.fields = fieldColl .Select(f => f.ToLower().Trim()) .ToArray(); } protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) { var property = base.CreateProperty(member, memberSerialization); property.ShouldSerialize = o => fields.Contains(member.Name.ToLower()); return property; } }

推荐答案

System.Text.Json 中的等效类型 -- JsonClassInfo 和 JsonPropertyInfo -- 是内部的.有一个开放的增强相当于 System.Text.Json 中的 DefaultContractResolver #31257要求公共等价物.– dbc 11 月 25 日 19:11

The equivalent types in System.Text.Json -- JsonClassInfo and JsonPropertyInfo -- are internal. There is an open enhancement Equivalent of DefaultContractResolver in System.Text.Json #31257 asking for a public equivalent. – dbc Nov 25 at 19:11

Github 问题:

  • 开放 System.Text.Json 的元数据基础结构 #34456
  • 相当于 System.Text.Json 中的 DefaultContractResolver.CreateProperties 覆盖 #60518
  • 相当于 System.Text.Json 中的 DefaultContractResolver #31257

请试试这个:我将其作为 System.Text.Json 的扩展编写,以提供缺失的功能:github/dahomey-technologies/Dahomey.Json.

您会发现对编程对象映射的支持.

You will find support for programmatic object mapping.

定义您自己的 IObjectMappingConvention 实现:

Define your own implementation of IObjectMappingConvention:

public class SelectiveSerializer : IObjectMappingConvention { private readonly IObjectMappingConvention defaultObjectMappingConvention = new DefaultObjectMappingConvention(); private readonly string[] fields; public SelectiveSerializer(string fields) { var fieldColl = fields.Split(','); this.fields = fieldColl .Select(f => f.ToLower().Trim()) .ToArray(); } public void Apply<T>(JsonSerializerOptions options, ObjectMapping<T> objectMapping) where T : class { defaultObjectMappingConvention.Apply<T>(options, objectMapping); foreach (IMemberMapping memberMapping in objectMapping.MemberMappings) { if (memberMapping is MemberMapping<T> member) { member.SetShouldSerializeMethod(o => fields.Contains(member.MemberName.ToLower())); } } } }

定义你的类:

public class Employee { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; } }

通过调用命名空间 Dahomey.Json 中定义的扩展方法 SetupExtensions 调用 JsonSerializerOptions 来设置 json 扩展:

Setup json extensions by calling on JsonSerializerOptions the extension method SetupExtensions defined in the namespace Dahomey.Json:

JsonSerializerOptions options = new JsonSerializerOptions(); options.SetupExtensions();

为类注册新的对象映射约定:

Register the new object mapping convention for the class:

options.GetObjectMappingConventionRegistry().RegisterConvention( typeof(Employee), new SelectiveSerializer("FirstName,Email,Id"));

然后使用常规 Sytem.Text.Json API 序列化您的类:

Then serialize your class with the regular Sytem.Text.Json API:

Employee employee = new Employee { Id = 12, FirstName = "John", LastName = "Doe", Email = "john.doe@acme" }; string json = JsonSerializer.Serialize(employee, options); // {"Id":12,"FirstName":"John","Email":"john.doe@acme"};

更多推荐

System.Text.Json API 是否有类似 IContractResolver 的东西

本文发布于:2023-11-17 02:40:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608507.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类似   东西   Text   System   Json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!