问题描述
我有一个用C#定义的枚举,将其值存储为字符,如下所示:
I have an enumeration defined with C#, where I'm storing it's values as characters, like this:
public enum CardType
{
Artist = 'A',
Contemporary = 'C',
Historical = 'H',
Musician = 'M',
Sports = 'S',
Writer = 'W'
}
我正在尝试使用JSON.NET反序列化,但是传入的JSON是使用CHAR值(字符串)而不是枚举的int值编写的,如下所示:
I'm attempting to deserialize using JSON.NET, but the incoming JSON was written using the CHAR value (string) instead of the int value of the enumeration, like this:
[{"CardType","A"},{"CardType", "C"}]
是否可以定义某种允许我手动将char解析为枚举值的转换器?
Is it possible to define some kind of converter that will allow me to manually parse the char to the enum value?
我尝试创建一个JsonConverter,但是不确定如何将其应用于此属性,而不应用于整个已解析的对象.这是我尝试过的:
I tried creating a JsonConverter, but am not sure how to do it, while applying it only to this property and not the whole parsed object. here's what I tried:
public class EnumerationConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, value);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
{
return null;
}
int value = serializer.Deserialize<int>(reader);
return (CardType)value;
}
public override bool CanConvert(Type objectType)
{
return objectType.IsSubclassOf(typeof(string));
}
}
逻辑可能是错误的,我可以解决这个问题,但是问题是根本没有调用ReadJson().
The logic might be wrong and I can fix that but the problem is ReadJson() isn't being called at all.
CanConvert是,但是似乎每个属性都调用了它,而不仅仅是我为其定义的一个属性:
CanConvert is, but it appears to be called for every property, not just the one property I defined it for:
public class Card
{
private CardType type;
[JsonConverter(typeof(EnumerationConverter))]
public CardType Type
{
get { return type; }
set { type = value; }
}
}
我确定我做错了,但是在查找有关如何在单个字段中执行此操作的文档时遇到了麻烦...
I'm sure I've done this incorrectly but am having trouble finding documentation on how to do this for a single field...
我想念什么?
推荐答案
您不需要自定义JsonConverter
,您可以将内置的StringEnumConverter
与 EnumMemberAttribute
(来自System.Runtime.Serialization
程序集).
You don't necessary need a custom JsonConverter
you can use the built in StringEnumConverter
with the combination of the EnumMemberAttribute
(from the System.Runtime.Serialization
assembly).
在没有EnumMemberAttribute
的情况下,它使用的是枚举名称,例如Artist,Contemporary等,因此您需要将其更改为A,C等值.
Without the EnumMemberAttribute
it uses the enum names so Artist, Contemporary, etc so you need to change the names with it to your A,C, etc value.
但这不是最好的解决方案,因为您必须重复两次值,但这是可行的:
But it is not the nicest solution because you have to repeat your values two times, but it works:
[JsonConverter(typeof(StringEnumConverter))]
public enum CardType
{
[EnumMember(Value = "A")]
Artist = 'A',
[EnumMember(Value = "C")]
Contemporary = 'C',
[EnumMember(Value = "H")]
Historical = 'H',
[EnumMember(Value = "M")]
Musician = 'M',
[EnumMember(Value = "S")]
Sports = 'S',
[EnumMember(Value = "W")]
Writer = 'W'
}
这篇关于将JSON字符反序列化为枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!