How to omit Get only properties in servicestack json serializer?

12,694

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

Share:
12,694
Hitesh.Aneja
Author by

Hitesh.Aneja

Senior technical architect at Headstrong LLC. New York. Designing and developing order entry applications for Morgan Stanley for last couple of years. I have experience in building software applications using microsoft technologies. Most of my experience is in web application development using ASP.Net, C#, VB.Net; ASP.Net MVC, Razor; WCF, IBM CICS; jQuery, EXTJs, AngularJS; SQL Server, IBM DB2. Programming is my passion and I love learning new technologies.

Updated on July 02, 2022

Comments

  • Hitesh.Aneja
    Hitesh.Aneja almost 2 years

    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

  • Hitesh.Aneja
    Hitesh.Aneja about 11 years
    Option 1 looks best for my scenario. Thanks for quick help.
  • Loudenvier
    Loudenvier over 10 years
    I must say that the third option is amazing! :-) When you have to deal with code you can't change that's the way to go. I also find it enormously elegant.
  • Faisal Mq
    Faisal Mq over 10 years
    Yeah good detail provided. And 3rd option is cool as it provides runtime control.
  • Erwin Mayer
    Erwin Mayer about 10 years
    [IgnoreDataMember] does not work with OrmLite, or did I miss something?
  • mythz
    mythz about 10 years
    @ErwinMayer Yeah it only applies to serialization. Use ServiceStack.DataAnnotations's [Ignore] attribute for OrmLite Data Models.
  • Kamarey
    Kamarey over 7 years
    @mythz: Would be very helpful to support something like this: JsConfig.IgnoreReadOnlyProperties = true;
  • Chris Bush
    Chris Bush over 5 years
    Sorry if this is common knowledge now, but I did discover that you can do this: JsConfig.IgnoreAttributesNamed = new[] { nameof(IgnoreAttribute) }; and that will let you use the same Ignore attribute you use for ORMLite.
  • Gábor
    Gábor over 5 years
    ShouldSerialize() doesn't receive the field name, it receives the JSON name, so if you use DataMember with Name, you have to supply that and keep track in both places. It would really be nice to have an attribute to specify incoming only or outgoing only.