gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 04:22:07 25 4
gpt4 key购买 nike

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

这是我试图移动的类(class)之一:

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;
}
}

最佳答案

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


Github 问题:
  • Open up metadata infrastructure of System.Text.Json #34456
  • Equivalent of DefaultContractResolver.CreateProperties override in System.Text.Json #60518
  • Equivalent of DefaultContractResolver in System.Text.Json #31257

  • 请试试这个:
    我将其作为 System.Text.Json 的扩展编写,以提供缺少的功能: https://github.com/dahomey-technologies/Dahomey.Json
    您将找到对编程对象映射的支持。
    定义您自己的 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 上调用命名空间 Dahomey.Json 中定义的扩展方法 SetupExtensions 来设置 json 扩展:
    JsonSerializerOptions options = new JsonSerializerOptions();
    options.SetupExtensions();
    为类注册新的对象映射约定:
    options.GetObjectMappingConventionRegistry().RegisterConvention(
    typeof(Employee), new SelectiveSerializer("FirstName,Email,Id"));
    然后使用常规 Sytem.Text.Json API 序列化您的类:
    Employee employee = new Employee
    {
    Id = 12,
    FirstName = "John",
    LastName = "Doe",
    Email = "john.doe@acme.com"
    };

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

    关于c# - System.Text.Json API 是否有类似 IContractResolver 的东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58926112/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com