How to ignore read-only properties (json)

Hi
Is there a way with the current json serializer to ignore read only properties? Or is there a way that I can hook into serialization for every property and then via reflection if I want the property or not?

There isn’t, you can find different ways to ignore properties in ServiceStack.Text serializers.

So you could add a ShouldSerialize() method on your DTOs to return

public bool? ShouldSerialize(string name) => GetType().GetProperty(name).CanWrite;

Or you could use reflection to go through and add the [IgnoreDataMember] attribute to read-only properties, e.g:

GetType()
    .GetPublicProperties()
    .Where(x => !x.CanWrite)
    .Each(x => x.AddAttributes(new IgnoreDataMemberAttribute()));

IMO the best option is to just use methods or public fields for accessing properties you don’t want serialized.

Question: The time duration of applying the IgnoreDataAttribute is until the application exit’s? Meaning this only needs to be done one time for all instances of the class?

Right, attributes (like most Configuration) should only be applied once on Startup and lasts until AppDomain shuts down.

Will IgnoreDataAttribute be recognized by Both ServiceStack.Text and ServiceStack.Ormlite?

My guess is no.

It appears that OrmLite has a different Ignore attribute called IgnoreAttribute.

OrmLite uses [Ignore] whilst ServiceStack.Text uses the Data Contracts [IgnoreDataMember] precisely so you can control which properties are ignored when saving with OrmLite or serializing with ServiceStack.Text.

Neither includes public fields by default (or methods) so you could use them to add fields you want ignored in either.