I have a TsEnum class where I store all my enum value. And I just added an extra value to the list but it will not deserialize a string. All other enum value deserialize fine just not the newest one I just added.
Code
var x = (SentencePartModelBase)JsonConvert.DeserializeObject(value.ToString(), type);
String Deserialize
{{
"Type": 204,
"Units": {
"Id": "41",
"Name": "mg",
"GroupName": "1"
},
"SequenceOrder": 2,
"IsInvalid": true
}}
[TsEnum]
public enum MaxDoseUnits
{
[EnumDisplayName("international units")]
[EnumGroupName("2")]
[EnumDictionaryId("f4ac115b-5bb1-4653-83af-3a4bef9a80e1")]
[EnumOrder("")]
Iu = 41,
}
CodePudding user response:
Just add [EnumMember(Value = "Id")] attribue to the enum
var x = JsonConvert.DeserializeObject<Root>(json);
classes
public enum MaxDoseUnits
{
[EnumMember(Value = "Iu")]
[EnumDisplayName("international units")]
[EnumGroupName("2")]
[EnumDictionaryId("f4ac115b-5bb1-4653-83af-3a4bef9a80e1")]
[EnumOrder("")]
Iu = 41
}
public class Units
{
public MaxDoseUnits Id { get; set; }
public string Name { get; set; }
public string GroupName { get; set; }
}
public class Root
{
public int Type { get; set; }
public Units Units { get; set; }
public int SequenceOrder { get; set; }
public bool IsInvalid { get; set; }
}
to test, you can deserialize this json without any problem too
{
"Type": 204,
"Units": {
"Id": "Iu", //or "Id":41, works too
"Name": "mg",
"GroupName": "1"
},
"SequenceOrder": 2,
"IsInvalid": true
}