简体   繁体   English

.NET Newtonsoft 允许仅序列化属性

[英].NET Newtonsoft Allow property to be only serialised

I have implemented the answer from this (rather old) question https://stackoverflow.com/a/31732029/4180176 but it doesn't seem to work for me and I can't figure out why.我已经实现了这个(相当老的)问题的答案https://stackoverflow.com/a/31732029/4180176但它似乎对我不起作用,我不知道为什么。

I can indeed see that property.Writable gets set to false.我确实可以看到property.Writable设置为 false。 The property I mark as not serializable gets put as the last field in the output json but is always present我标记为不可序列化的属性被放置为输出 json 中的最后一个字段,但始终存在

Startup.cs启动.cs

services.AddControllers().AddNewtonsoftJson(opt =>
{
  opt.UseMemberCasing();
  opt.SerializerSettings.ContractResolver = new NewtonsoftContractResolver();
});

Contract Resolver合约解析器

[AttributeUsage(AttributeTargets.Property)]
public class DoNotSerializeAttribute : Attribute
{

}

public class NewtonsoftContractResolver : DefaultContractResolver
{
    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {
        var property = base.CreateProperty(member, memberSerialization);

        if (property is not null && property.Writable)
        {
            var attributes = property.AttributeProvider.GetAttributes(typeof(DoNotSerializeAttribute), true);

            if (attributes is not null && attributes.Count > 0)
            {
                    property.Writable = false;
            }
        }    
        return property;
    }
}

Attribute Usage属性使用

public class LeaderboardUser : RankedObject
{
    [JsonProperty("UserID")]
    [DoNotSerialize]
    public string UserID { get; set; }
}

serialization序列化

var leaderBoardUser = new LeaderboardUser { UserID = "userId", UserName = "userName" };
var json = JsonConvert.SerializeObject(leaderBoardUser);

result结果

{"UserID":"userId","UserName":"userName"}

deserialization反序列化

leaderBoardUser = JsonConvert.DeserializeObject<LeaderboardUser>(json);

result结果

UserID  null
UserName    userName

class班级

public partial class LeaderboardUser
{
    public string UserID { get; set; }
    public string UserName { get; set; }

    [JsonConstructor]
    public LeaderboardUser(string UserId)
    {

    }
    public LeaderboardUser()
    {

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Newtonsoft Json.NET 不序列化 [Obsolete] 属性 - Newtonsoft Json.NET do not serialize [Obsolete] property Newtonsoft Rename 扩展方法重命名 JObject .net 中的属性名称 - Newtonsoft Rename extension method to rename a property name in JObject .net JSON序列化继承的列表类属性未序列化 - JSON Serialisation Inherited List Class Property Not Serialised Newtonsoft Json.NET 反序列化仅设置 Json 字符串中的值 - Newtonsoft Json.NET Deserialize Setting Only Values in Json String 使用Newtonsoft Json.NET仅序列化派生类型 - Serialize only derived type using Newtonsoft Json.NET Newtonsoft 未序列化导航属性 - Newtonsoft is not serializing navigation property C#使用Newtonsoft.json对仅一个属性(找不到根)反序列化Json响应 - C# Deserialize Json response for only one property (could't find root) using Newtonsoft.json Newtonsoft.json 或 System.Text.Json 仅序列化属性的定义类型而不是继承值 - Newtonsoft.json or System.Text.Json Serialize only the property's defined type not inherited values C# 在 Newtonsoft JSON.Net 中搜索属性并返回其值 - C# Searching for a property and returning its value in Newtonsoft JSON.Net 使用Newtonsoft的JSON.NET处理C#中JSON对象的名为“ private”的属性 - Dealing with a JSON object's property called “private” in C# with Newtonsoft's JSON.NET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM