I am working on a JSON driven project and I would like to provide the SessionManager
object with a dynamic list of permissionst. While I can work with an array of key value pairs for permissions, I was wondering if I could remove the property names so that the key is the Permission
value and the value is the IsAllowed
value.
public class SessionPermission
{
public string Permission { get; set; }
public bool IsAllowed { get; set; }
}
public class SessionManager
{
public string UserName { get; set; }
public string Password { get; set; }
public List<SessionPermission> Permissions { get; set; }
public void SetPermissions()
{
Permissions = new List<SessionPermission>
{
new SessionPermission {Permission = "CreateUsers", IsAllowed = false},
new SessionPermission {Permission = "EditUsers", IsAllowed = false},
new SessionPermission {Permission = "EditBlog", IsAllowed = true}
};
}
}
When I generate JSON it outputs an array of permissions:
{
"Permission": "CreateUsers",
"IsAllowed": false
},
I would like to know how to override the serialization so that it uses the values instead of the property names.
{
"CreateUsers": false
},
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…