如何忽略仅在Servicestack JSON序列化程序中获取属性? [英] How to omit Get only properties in servicestack json serializer?
问题描述
我有一个对象,该对象使用ServiceStack.Text命名空间中的ToJson<>()
方法反序列化.
I have an object which I am de-serializing using ToJson<>()
method from ServiceStack.Text namespace.
如何在序列化过程中忽略所有仅GET
属性?有没有像[Ignore]
这样的属性或可以用来装饰属性的东西,以便可以将其省略?
How to omit all the GET
only propeties during serialization? Is there any attribute like [Ignore]
or something that I can decorate my properties with, so that they can be omitted?
谢谢
推荐答案
ServiceStack的文本序列化器紧随其后.NET的DataContract序列化程序行为,这意味着您可以通过使用opt-out [IgnoreDataMember]
属性
ServiceStack's Text serializers follows .NET's DataContract serializer behavior, which means you can ignore data members by using the opt-out [IgnoreDataMember]
attribute
public class Poco
{
public int Id { get; set; }
public string Name { get; set; }
[IgnoreDataMember]
public string IsIgnored { get; set; }
}
一种选择加入的方法是装饰要使用[DataMember]
序列化的每个属性.其余属性未序列化,例如:
An opt-in alternative is to decorate every property you want serialized with [DataMember]
. The remaining properties aren't serialized, e.g:
[DataContract]
public class Poco
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
public string IsIgnored { get; set; }
}
最后,还有一个不需要属性的非侵入式选项,例如:
Finally there's also a non-intrusive option that doesn't require attributes, e.g:
JsConfig<Poco>.ExcludePropertyNames = new [] { "IsIgnored" };
动态指定应序列化的属性
ServiceStack的序列化器还通过提供常规命名的ShouldSerialize({PropertyName})
方法来指示是否应序列化属性,从而支持动态控制序列化,例如:
Dynamically specifying properties that should be serialized
ServiceStack's Serializers also supports dynamically controlling serialization by providing conventionally named ShouldSerialize({PropertyName})
methods to indicate whether a property should be serialized or not, e.g:
public class Poco
{
public int Id { get; set; }
public string Name { get; set; }
public string IsIgnored { get; set; }
public bool? ShouldSerialize(string fieldName)
{
return fieldName == "IsIgnored";
}
}
ConditionalSerializationTests中的更多示例.cs
这篇关于如何忽略仅在Servicestack JSON序列化程序中获取属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!