Let's imagine, that I have a class
public class Foo
{
public int Prop1 { get; set; }
public int Prop2 { get; set; }
public int Prop3 { get; set; }
}
And imagine, that in some controller FooController I'm creating a List<> of such classes Foo, populating it with data, serializing it with Newtonsoft.Json and sending to client.
It's okay, there is no problems.
But, also I have per-user permissions system, which says that User1 can't see data of Prop1 and User2 can't see data of Prop3. And I have a lot of such classes Foo and a lot of permissions for different users of my system. And, to disallow users to see data from not allowed columns I decided to interrupt json serialization and exclude not allowed for user colums from JSON serialization.
For the moment it is already written custom JsonConverter, which allow me to do so. But, it is complicated (input class scan, dynamic accessor compilation, recursion and etc) and comparably to native newtonsoft's, slow.
Concerning above facts I want to ask if there is a easier way to achive the desired result? I mean, without creating custom JsonConverter remove any column from any serialized with json class.
Thanks for the answers!
UPDATE Followed the answer by @SebastianStehle. Extended my own mapper to map class to dictionary with an ability to exclude fields.