序列化时如何自动忽略没有相应构造函数参数的所有属性
How to auto-ignore all properties with no corresponding constructor parameter when serializing
是否有一些非基于属性的方法可以在序列化时忽略所有没有相应构造函数参数的属性?例如,当序列化这个 class 时, 属性 Combo
应该被忽略。 MyClass
实例的往返 serialization/deserialization 不需要序列化 Combo
。理想情况下,我可以使用一些开箱即用的设置。
public class MyClass
{
public MyClass(int myInt, string myString)
{
this.MyInt = myInt;
this.MyString = myString;
}
public int MyInt { get; }
public string MyString { get; }
public string Combo => this.MyInt + this.MyString;
}
您可以使用 custom IContractResolver
:
public class ConstructorPropertiesOnlyContractResolver : DefaultContractResolver
{
readonly bool serializeAllWritableProperties;
public ConstructorPropertiesOnlyContractResolver(bool serializeAllWritableProperties)
: base()
{
this.serializeAllWritableProperties = serializeAllWritableProperties;
}
protected override JsonObjectContract CreateObjectContract(Type objectType)
{
var contract = base.CreateObjectContract(objectType);
if (contract.CreatorParameters.Count > 0)
{
foreach (var property in contract.Properties)
{
if (contract.CreatorParameters.GetClosestMatchProperty(property.PropertyName) == null)
{
if (!serializeAllWritableProperties || !property.Writable)
property.Readable = false;
}
}
}
return contract;
}
}
然后像这样使用它:
var settings = new JsonSerializerSettings { ContractResolver = new ConstructorPropertiesOnlyContractResolver(false) };
var json = JsonConvert.SerializeObject(myClass, Formatting.Indented, settings );
如果您还想序列化未包含在构造函数参数列表中的 read/write 属性,请将 true
传递给 serializeAllWritableProperties
,例如AnUnrelatedReadWriteProperty
在:
public class MyClass
{
public MyClass(int myInt, string myString)
{
this.MyInt = myInt;
this.MyString = myString;
}
public int MyInt { get; private set; }
public string MyString { get; private set; }
public string Combo { get { return this.MyInt + this.MyString; } }
public string AnUnrelatedReadWriteProperty { get; set; }
}
请注意,您可能需要 。
是否有一些非基于属性的方法可以在序列化时忽略所有没有相应构造函数参数的属性?例如,当序列化这个 class 时, 属性 Combo
应该被忽略。 MyClass
实例的往返 serialization/deserialization 不需要序列化 Combo
。理想情况下,我可以使用一些开箱即用的设置。
public class MyClass
{
public MyClass(int myInt, string myString)
{
this.MyInt = myInt;
this.MyString = myString;
}
public int MyInt { get; }
public string MyString { get; }
public string Combo => this.MyInt + this.MyString;
}
您可以使用 custom IContractResolver
:
public class ConstructorPropertiesOnlyContractResolver : DefaultContractResolver
{
readonly bool serializeAllWritableProperties;
public ConstructorPropertiesOnlyContractResolver(bool serializeAllWritableProperties)
: base()
{
this.serializeAllWritableProperties = serializeAllWritableProperties;
}
protected override JsonObjectContract CreateObjectContract(Type objectType)
{
var contract = base.CreateObjectContract(objectType);
if (contract.CreatorParameters.Count > 0)
{
foreach (var property in contract.Properties)
{
if (contract.CreatorParameters.GetClosestMatchProperty(property.PropertyName) == null)
{
if (!serializeAllWritableProperties || !property.Writable)
property.Readable = false;
}
}
}
return contract;
}
}
然后像这样使用它:
var settings = new JsonSerializerSettings { ContractResolver = new ConstructorPropertiesOnlyContractResolver(false) };
var json = JsonConvert.SerializeObject(myClass, Formatting.Indented, settings );
如果您还想序列化未包含在构造函数参数列表中的 read/write 属性,请将 true
传递给 serializeAllWritableProperties
,例如AnUnrelatedReadWriteProperty
在:
public class MyClass
{
public MyClass(int myInt, string myString)
{
this.MyInt = myInt;
this.MyString = myString;
}
public int MyInt { get; private set; }
public string MyString { get; private set; }
public string Combo { get { return this.MyInt + this.MyString; } }
public string AnUnrelatedReadWriteProperty { get; set; }
}
请注意,您可能需要