C# 如何在值为空的字典中添加默认值
我有一个枚举属性C# 如何在值为空的字典中添加默认值,c#,entity-framework,dictionary,.net-core,C#,Entity Framework,Dictionary,.net Core,我有一个枚举属性 public enum UserNotificationTypes { Email, SMS, Push_Messages } 还有一个模范班 public class SaveUserSettingRequest { public string UserName { get; set; } public Dictionary<string, string> UserNotifications { get; set; }
public enum UserNotificationTypes
{
Email,
SMS,
Push_Messages
}
还有一个模范班
public class SaveUserSettingRequest
{
public string UserName { get; set; }
public Dictionary<string, string> UserNotifications { get; set; }
}
听到我的功能运行良好。我的问题是,如果键值为空,我希望插入带有false的数据
{
"UserName": "xyz",
"UserNotifications": {
"Email": "",
"SMS": "",
"Push_Messages": ""
}
}
基本上,您试图设置为false,如果您的settings.Value为空,您可以尝试下面的代码
usettings.Value = string.IsNullOrEmpty(settings.Value) ? "false" : settings.Value;
查看您的案例中的JsonConverter实现,这是一种无缝的方式来实现您想要做的事情,因为它将在反序列化过程中对Json进行内部转换,而无需在业务逻辑中添加任何额外的代码。这是一种基于属性的编程,它为属性添加了一个额外的转换方面,在读写Json中,任何数量的自定义逻辑都可以在序列化和反序列化过程中进行合并
void Main()
{
string json = "{\"UserName\":\"xyz\",\"UserNotifications\":{\"Email\":\"\",\"SMS\":\"\",\"Push_Messages\":\"\"}}";
var result = JsonConvert.DeserializeObject<UserSetting>(json);
result.Dump();
}
// Create a Custom JsonConverter
public class UserNotificationsConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(UserNotifications).IsAssignableFrom(objectType);
}
// Custom logic in the ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var userNotificationObj = serializer.Deserialize<UserNotifications>(reader);
userNotificationObj.Email = string.IsNullOrEmpty(userNotificationObj.Email) ? "false" : userNotificationObj.Email;
userNotificationObj.SMS = string.IsNullOrEmpty(userNotificationObj.SMS) ? "false" : userNotificationObj.SMS;
userNotificationObj.Push_Messages = string.IsNullOrEmpty(userNotificationObj.Push_Messages) ? "false" : userNotificationObj.Push_Messages;
return userNotificationObj;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
// UserSetting entity
public class UserSetting
{
public string UserName {get; set;}
// Decorate with the Custom Json Converter
[JsonConverterAttribute(typeof(UserNotificationsConverter))]
public UserNotifications UserNotifications {get; set;}
}
public class UserNotifications
{
public string Email {get; set;}
public string SMS {get; set;}
public string Push_Messages {get; set;}
}
结果:
prasad上面的代码行运行良好,但在true时出错。请求..like字符串不包含定义请求..现在按照我的要求工作..谢谢prasad garu..因为您正在使用Json数据,所以在反序列化时,您应该能够应用自定义转换器将空值转换为false。这样,您就不会将非业务逻辑与主代码Welcome@CherryR混合,这是使用编程实现它的正确方法,因此在您的实际代码中实现转换器并在内部管理转换这是在根级别解决问题的正确方法,@CherryR这将帮助我们在进一步开发中处理此类情况+感谢@prasadelkikikar,查看链接-了解更多信息以及JsonConverter和ContractResolver之间的区别,它们可以在类级别使用
usettings.Value = string.IsNullOrEmpty(settings.Value) ? "false" : settings.Value;
void Main()
{
string json = "{\"UserName\":\"xyz\",\"UserNotifications\":{\"Email\":\"\",\"SMS\":\"\",\"Push_Messages\":\"\"}}";
var result = JsonConvert.DeserializeObject<UserSetting>(json);
result.Dump();
}
// Create a Custom JsonConverter
public class UserNotificationsConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(UserNotifications).IsAssignableFrom(objectType);
}
// Custom logic in the ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var userNotificationObj = serializer.Deserialize<UserNotifications>(reader);
userNotificationObj.Email = string.IsNullOrEmpty(userNotificationObj.Email) ? "false" : userNotificationObj.Email;
userNotificationObj.SMS = string.IsNullOrEmpty(userNotificationObj.SMS) ? "false" : userNotificationObj.SMS;
userNotificationObj.Push_Messages = string.IsNullOrEmpty(userNotificationObj.Push_Messages) ? "false" : userNotificationObj.Push_Messages;
return userNotificationObj;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
// UserSetting entity
public class UserSetting
{
public string UserName {get; set;}
// Decorate with the Custom Json Converter
[JsonConverterAttribute(typeof(UserNotificationsConverter))]
public UserNotifications UserNotifications {get; set;}
}
public class UserNotifications
{
public string Email {get; set;}
public string SMS {get; set;}
public string Push_Messages {get; set;}
}