1

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?

Johan Driessen
  • 841
  • 2
  • 8
  • 13

1 Answers1

4

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.

mythz
  • 141,670
  • 29
  • 246
  • 390
  • Great, that solved to problem for us! But if I may say so, I think it is wrong that the Json serializer in ServiceStack.Text uses the JsonIgnore-attribute from Json.NET as the default setting. While I agree that it could be useful in some cases, it would make more sense if it was opt-in. You could easily just reverse the code you just added and instead have JsConfig.UseIgnoreAttributesNamed or someething like that. That would also make it possible to be compatible with other serializers than Json.NET. – Johan Driessen Aug 20 '14 at 12:08
  • @JohanDriessen The attribute is `[JsonIgnore]` not `[JsonNetIgnore]` if you see those attributes on a model, without any external knowledge it would naturally be assumed it's a directive for the chosen JSON Serializer to ignore that property. Having it built-in as a default helps with portability, at the cost of this particular scenario which I'm recommending against as it doesn't encourage "Pit of Success" development and is likely to trip someone else in future. – mythz Aug 20 '14 at 12:15