c# JSON 序列化使用值而不是 属性 名称
c# JSON Serialization Use Value Instead of Property Name
我正在开发一个 JSON 驱动的项目,我想为 SessionManager
对象提供一个动态的权限列表。虽然我可以使用一组键值对来获得权限,但我想知道是否可以删除 属性 名称,以便 key 是 Permission
值值是IsAllowed
值。
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}
};
}
}
当我生成 JSON 时,它输出一组权限:
{
"Permission": "CreateUsers",
"IsAllowed": false
},
我想知道如何覆盖序列化,以便它使用值而不是 属性 名称。
{
"CreateUsers": false
},
您可以使用以下自定义转换器:
public class SessionPermissionConverter : JsonConverter
{
public override object ReadJson(
JsonReader reader,
Type objectType,
object existingValue,
JsonSerializer serializer)
{
var obj = (JObject)JObject.ReadFrom(reader);
JProperty property = obj.Properties().FirstOrDefault();
return new SessionPermission
{
Permission = property.Name,
IsAllowed = property.Value.Value<bool>()
};
}
public override void WriteJson(
JsonWriter writer,
object value,
JsonSerializer serializer)
{
SessionPermission permission = (SessionPermission)value;
JObject obj = new JObject();
obj[permission.Permission] = permission.IsAllowed;
obj.WriteTo(writer);
}
public override bool CanConvert(Type t)
{
return typeof(SessionPermission).IsAssignableFrom(t);
}
public override bool CanRead
{
get { return true; }
}
}
用法:
var manager = new SessionManager();
manager.SetPermissions();
string json = JsonConvert.SerializeObject(manager, new SessionPermissionConverter());
样本JSON:
{
"UserName": null,
"Password": null,
"Permissions": [
{
"CreateUsers": false
},
{
"EditUsers": false
},
{
"EditBlog": true
}
]
}
反之亦然。
我正在开发一个 JSON 驱动的项目,我想为 SessionManager
对象提供一个动态的权限列表。虽然我可以使用一组键值对来获得权限,但我想知道是否可以删除 属性 名称,以便 key 是 Permission
值值是IsAllowed
值。
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}
};
}
}
当我生成 JSON 时,它输出一组权限:
{
"Permission": "CreateUsers",
"IsAllowed": false
},
我想知道如何覆盖序列化,以便它使用值而不是 属性 名称。
{
"CreateUsers": false
},
您可以使用以下自定义转换器:
public class SessionPermissionConverter : JsonConverter
{
public override object ReadJson(
JsonReader reader,
Type objectType,
object existingValue,
JsonSerializer serializer)
{
var obj = (JObject)JObject.ReadFrom(reader);
JProperty property = obj.Properties().FirstOrDefault();
return new SessionPermission
{
Permission = property.Name,
IsAllowed = property.Value.Value<bool>()
};
}
public override void WriteJson(
JsonWriter writer,
object value,
JsonSerializer serializer)
{
SessionPermission permission = (SessionPermission)value;
JObject obj = new JObject();
obj[permission.Permission] = permission.IsAllowed;
obj.WriteTo(writer);
}
public override bool CanConvert(Type t)
{
return typeof(SessionPermission).IsAssignableFrom(t);
}
public override bool CanRead
{
get { return true; }
}
}
用法:
var manager = new SessionManager();
manager.SetPermissions();
string json = JsonConvert.SerializeObject(manager, new SessionPermissionConverter());
样本JSON:
{
"UserName": null,
"Password": null,
"Permissions": [
{
"CreateUsers": false
},
{
"EditUsers": false
},
{
"EditBlog": true
}
]
}
反之亦然。