System.Text.Json API类似于IContractResolver [英] System.Text.Json API is there something like IContractResolver

查看:242
本文介绍了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的扩展,以提供缺少的功能: https://github.com/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.com"
};

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

这篇关于System.Text.Json API类似于IContractResolver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆