How to omit Get only properties in servicestack json serializer?

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

Answers


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; }
}

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" };
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";
    }
}

More examples in ConditionalSerializationTests.cs


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:

    public SignInPostResponse Post(SignInPost request)
    {
        UserAuthentication auth = _userService.SignIn(request.Domain, true, request.Username, request.Password);

        // Map domain model ojbect to API model object. These classes are used with several API calls.
        var webAuth = Map<WebUserAuthentication>(auth);

        // Exmaple: Clear a property that I don't want to return for this API call... for whatever reason.
        webAuth.AuthenticationType = null;

        var response = new SignInPostResponse { Results = webAuth };
        return response;
    }

I do wish there was a way to dynamically control the serialization of all members (including non-nullable) on a per endpoint fashion.


Need Your Help

Haskell and random numbers

haskell random monads

I've been messing with Haskell few days and stumbled into a problem.

Unescape HTML entities in Javascript?

javascript html escaping xml-rpc

I have some Javascript code that communicates with an XML-RPC backend.