我有一个DTO类,我序列化

Json.Serialize(MyClass)

我如何排除该物业的公共财产?

(它必须是公共的,因为我在其他地方的代码中使用它)


当前回答

如果你正在使用System.Text.Json,那么你可以使用[JsonIgnore]。 FQ: System.Text.Json.Serialization.JsonIgnoreAttribute

官方微软文档:JsonIgnoreAttribute

如下所述:

该库是作为. net Core 3.0共享框架的一部分内置的。 对于其他目标框架,安装System.Text.Json NuGet 包中。该软件包支持: .NET标准2.0及更高版本 .NET Framework 4.6.1及以上版本 .NET Core 2.0, 2.1和2.2

其他回答

如果你使用Json。Net属性[JsonIgnore]将在序列化或反序列化时简单地忽略字段/属性。

public class Car
{
  // included in JSON
  public string Model { get; set; }
  public DateTime Year { get; set; }
  public List<string> Features { get; set; }

  // ignored
  [JsonIgnore]
  public DateTime LastModified { get; set; }
}

或者你可以使用DataContract和DataMember属性来有选择地序列化/反序列化属性/字段。

[DataContract]
public class Computer
{
  // included in JSON
  [DataMember]
  public string Name { get; set; }
  [DataMember]
  public decimal SalePrice { get; set; }

  // ignored
  public string Manufacture { get; set; }
  public int StockCount { get; set; }
  public decimal WholeSalePrice { get; set; }
  public DateTime NextShipmentDate { get; set; }
}

详情请参阅http://james.newtonking.com/archive/2009/10/23/efficient-json-with-json-net-reducing-serialized-json-size

如果你正在使用System.Text.Json,那么你可以使用[JsonIgnore]。 FQ: System.Text.Json.Serialization.JsonIgnoreAttribute

官方微软文档:JsonIgnoreAttribute

如下所述:

该库是作为. net Core 3.0共享框架的一部分内置的。 对于其他目标框架,安装System.Text.Json NuGet 包中。该软件包支持: .NET标准2.0及更高版本 .NET Framework 4.6.1及以上版本 .NET Core 2.0, 2.1和2.2

抱歉,我决定再写一个答案,因为其他答案都不能复制粘贴。

如果你不想用一些属性装饰属性,或者如果你无法访问类,或者如果你想决定在运行时序列化什么,等等,等等,这里是你如何在Newtonsoft做它。Json

//short helper class to ignore some properties from serialization
public class IgnorePropertiesResolver : DefaultContractResolver
{
    private readonly HashSet<string> ignoreProps;
    public IgnorePropertiesResolver(IEnumerable<string> propNamesToIgnore)
    {
        this.ignoreProps = new HashSet<string>(propNamesToIgnore);
    }

    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {
        JsonProperty property = base.CreateProperty(member, memberSerialization);
        if (this.ignoreProps.Contains(property.PropertyName))
        {
            property.ShouldSerialize = _ => false;
        }
        return property;
    }
}

使用

JsonConvert.SerializeObject(YourObject, new JsonSerializerSettings()
        { ContractResolver = new IgnorePropertiesResolver(new[] { "Prop1", "Prop2" }) });

注意:如果您决定使用这个答案,请确保缓存ContractResolver对象,否则性能可能会受到影响。

我在这里发布了代码,以防有人想要添加任何东西

https://github.com/jitbit/JsonIgnoreProps

您也可以使用[NonSerialized]属性

[Serializable]
public struct MySerializableStruct
{
    [NonSerialized]
    public string hiddenField;
    public string normalField;
}

来自微软文档:

指示可序列化类的字段不应被序列化。这个类不能被继承。


例如,如果你使用Unity(这不仅仅适用于Unity),那么这适用于UnityEngine。JsonUtility

using UnityEngine;

MySerializableStruct mss = new MySerializableStruct 
{ 
    hiddenField = "foo", 
    normalField = "bar" 
};
Debug.Log(JsonUtility.ToJson(mss)); // result: {"normalField":"bar"}

对于c# 9的记录,它是[property: JsonIgnore]

using System.Text.Json.Serialization;

public record R(
   string Text2
   [property: JsonIgnore] string Text2)

对于经典样式,它仍然只是[JsonIgnore]。

using System.Text.Json.Serialization;

public record R
{
   public string Text {get; init; }

   [JsonIgnore] 
   public string Text2 { get; init; }
}