我反序列化json屬性到一個枚舉,但我遇到問題處理的情況下,當屬性是一個空字符串。如何處理json.net中的空字符串到枚舉的反序列化
錯誤轉換價值 「」 輸入 'EnrollmentState'
我想反序列化的requiredItem
的state
財產。
{
"currentStage" : "Pre-Approved",
"stages" : ["Applicant", "Pre-Approved", "Approved", "Enrolled"],
"requiredItems" : [{
"id" : 1,
"name" : "Documents",
"state" : ""
}, {
"id" : 2,
"name" : "Eligibility Verification",
"state" : "complete"
}, {
"id" : 3,
"name" : "Placement Information",
"state" : "incomplete"
}
]
}
RequiredItem
類和枚舉...
public class RequiredItem {
/// <summary>
/// Gets or sets the identifier.
/// </summary>
/// <value>The identifier.</value>
public string id { get; set; }
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>The name.</value>
public string name { get; set; }
/// <summary>
/// Gets or sets the status.
/// </summary>
/// <value>The status.</value>
[JsonProperty(ItemConverterType = typeof(StringEnumConverter))]
public EnrollmentState state { get; set; }
}
[JsonConverter(typeof(StringEnumConverter))]
public enum EnrollmentState {
[EnumMember(Value = "incomplete")]
Incomplete,
[EnumMember(Value = "actionNeeded")]
ActionNeeded,
[EnumMember(Value = "complete")]
Complete
}
如何我可以設置反序列化的默認值,這樣空字符串將被反序列化爲EnrollmentState.Incomplete
而不是拋出一個運行時錯誤的?
你能不能只爲空字符串添加一個處理程序到EnrollmentState枚舉? – Maess