Ignore a property when deserializing using Json.Net with ItemRequired = Required.Always

前端 未结 1 1108
一整个雨季
一整个雨季 2020-12-10 02:30

I\'m using Json.Net to serialize and deserialize classes to json and back.

I added to a class marked with [JsonObject(ItemRequired = Required.Always)] (

相关标签:
1条回答
  • 2020-12-10 03:07

    Evidently JsonIgnore will only control the serialization in this case. JsonIgnore is required to specify that the FullName property should not be serialized to the json representation.

    To ignore the property during deserialization we need to add the JsonProperty annotation with Required = Required.Default (which means not required).

    So, this is how to avoid the JsonSerializationException:

    [JsonObject(ItemRequired = Required.Always)]
    public class Hamster
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        [JsonIgnore]
        [JsonProperty(Required = Required.Default)]
        public string FullName { get { return FirstName + LastName; }}
    }
    
    0 讨论(0)
提交回复
热议问题