Search code examples
.netserializationjson.netservicestack-textservicestack.redis

Is it possible to make the ServiceStack Json serializer serialize properties marked with JsonIgnore


We have an application were we're using ServiceStack.Redis for caching. The application is a REST API, where we serve the responses in JSON. We're using Newtonsoft JSON.NET to serialize our responses, and of course ServiceStack.Redis uses ServiceStack.Text to serialize/deserialize the data being stored in the Redis cache.

The problem is that we want to store more data in the cache than we serve to the clients. So we've been marking properties that we don't want to expose to the clients with the [JsonIgnore] attribute from JSON.NET. This worked great in ServiceStack.Redis 4.0.20, but broke when we upgraded to 4.0.30. It seems they changed the behaviour of the Json Serializer to respect the JsonIgnore attribute from JSON.NET (https://github.com/ServiceStack/ServiceStack.Text/pull/420).

So if we have the class

public class FooBar 
{
    public string Foo { get; set; }

    [JsonIgnore]
    public string Bar { get; set; }
}

and try to serialize the object

var fooBar = new FooBar { Foo = "A", Bar = "B" }

what used to be stored in Redis was

{ "Foo": "A", "Bar": "B" }

but now we lose the Bar and only get

{ "Foo": "A" }

Is there any way to make ServiceStack serialize the properties that are marked with JsonIgnore, or do we need to go back to the previous version (and maybe be stuck on the previous version forever)?

Alternatively, is there a good way to make JSON.NET ignore certain properties without using JsonIgnore?


Solution

  • Whilst I don't recommend relying on the different of behavior between serializers, I've just added a commit that lets you change what Attributes are used to ignore properties. i.e. you can remove [JsonIgnore] attribute with:

    JsConfig.IgnoreAttributesNamed = new[] {
        typeof(IgnoreDataMemberAttribute).Name //i.e. Remove [JsonIgnore] 
    };
    

    This is available from v4.0.31+ that's now available on MyGet.