【问题标题】:Json.net serialize only certain propertiesJson.net 仅序列化某些属性
【发布时间】:2023-03-29 15:18:01
【问题描述】:

Json.net 有没有办法只指定你想要序列化的属性?或者基于像 Declared Only 这样的绑定标志序列化某些属性?

现在我正在使用JObject.FromObject(MainObj.SubObj); 来获取 SubObj 的所有属性,它是一个遵循 ISubObject 接口的类的实例:

public interface ISubObject
{

}

public class ParentSubObject : ISubObject
{
    public string A { get; set; }
}


public class SubObjectWithOnlyDeclared : ParentSubObject
{
    [JsonInclude] // This is fake, but what I am wishing existed
    public string B { get; set; }

    [JsonInclude] // This is fake, but what I am wishing existed
    public string C { get; set; }
}

public class NormalSubObject: ParentSubObject
{
    public string B { get; set; }
}

如果MainObj.SubObj 是NormalSubObject,它将同时对A 和B 进行序列化,但如果是SubObjectWithOnlyDeclared,它将仅对B 和C 进行序列化并忽略父属性

【问题讨论】:

标签: c# serialization attributes json.net


【解决方案1】:

而不必像另一个答案中所建议的那样,在您不想序列化的每个属性上使用[JsonIgnore]。

如果您只想指定要序列化的属性,您可以这样做,使用[JsonObject(MemberSerialization.OptIn)] 和[JsonProperty] 属性,如下所示:

using Newtonsoft.Json;
...
[JsonObject(MemberSerialization.OptIn)]
public class Class1
{
    [JsonProperty]
    public string Property1 { set; get; }
    public string Property2 { set; get; }
}

这里只有Property1会被序列化。

【讨论】:

  • 这应该是选择的答案
【解决方案2】:

你可以像下面这样编写一个自定义的 ContractResolver

public class IgnoreParentPropertiesResolver : DefaultContractResolver
{
    bool IgnoreBase = false;
    public IgnoreParentPropertiesResolver(bool ignoreBase)
    {
        IgnoreBase = ignoreBase;
    }
    protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
    {
        var allProps = base.CreateProperties(type, memberSerialization);
        if (!IgnoreBase) return allProps;

        //Choose the properties you want to serialize/deserialize
        var props = type.GetProperties(~BindingFlags.FlattenHierarchy); 

        return allProps.Where(p => props.Any(a => a.Name == p.PropertyName)).ToList();
    }
}

现在您可以在序列化过程中将其用作:

var settings = new JsonSerializerSettings() { 
                      ContractResolver = new IgnoreParentPropertiesResolver(true) 
               };
var json1 = JsonConvert.SerializeObject(new SubObjectWithOnlyDeclared(),settings );

【讨论】:

  • 谢谢,正是我要找的东西
【解决方案3】:

不知道为什么@Eser 选择将答案作为对您问题的评论而不是实际答案...无论如何,它们是正确的。

将[JsonIgnore] 属性应用于您要忽略的任何属性。

【讨论】:

  • 虽然这不是一个可接受的答案,但我需要有条件地忽略 public string A { get; set; },因为在一种情况下我不想忽略它,而在另一种情况下我愿意。
【解决方案4】:

如果你的对象有一个属性为 null 或默认值,你可以让 json.net 忽略它,而不是通过以下方式序列化它:

var settings = new JsonSerializerSettings 
{
    DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore
};

JsonConvert.SerializeObject(myObject, settings);

编辑:

或全局默认设置,执行一次:

JsonConvert.DefaultSettings = () => new JsonSerializerSettings 
{
    DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore
};

【讨论】:

  • 非常感谢..这就是我所期待的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-27
  • 1970-01-01
  • 1970-01-01
  • 2012-04-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-12
相关资源
最近更新 更多