System.Text.Json API类似于IContractResolver

编程入门 行业动态 更新时间:2024-10-26 12:34:30
本文介绍了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#42001 中有一个开放的DefaultContractResolver等效要求公开等效项。 – 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 #42001 asking for a public equivalent. – dbc Nov 25 at 19:11

Github问题

Github Issue

请尝试以下操作: 我将其编写为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; } }

通过调用JsonSerializerOptions来设置JSON扩展名,该扩展名方法是在命名空间中定义的Dahomey.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-03 13:05:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555203.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类似于   Text   System   Json   IContractResolver

发布评论

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

>www.elefans.com

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