I have an object which I am de-serializing using ToJson<>()
method from ServiceStack.Text namespace.
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?
Thanks
For nullable members, you also have the ability to set it to null before serializing.
This is particularly useful if you want to create a single view/api model that is re-used for several API calls. The service can touch it up before setting it on the response object.
Example:
I do wish there was a way to dynamically control the serialization of all members (including non-nullable) on a per endpoint fashion.
ServiceStack's Text serializers follows .NET's DataContract serializer behavior, which means you can ignore data members by using the opt-out
[IgnoreDataMember]
attributeAn opt-in alternative is to decorate every property you want serialized with
[DataMember]
. The remaining properties aren't serialized, e.g:Finally there's also a non-intrusive option that doesn't require attributes, e.g:
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:More examples in ConditionalSerializationTests.cs