Deserializing public property with non-public setter in json.net

后端 未结 3 902
栀梦
栀梦 2020-12-10 16:13

Suppose I have the following class -

public class A 
{        
   public int P1 { get; internal set; }
}

Using json.net, I am able to seria

3条回答
  •  一个人的身影
    2020-12-10 16:33

    After some experimenting, I've found that a property is deserialised correctly if you decorate your property with:

    [JsonProperty(DefaultValueHandling = DefaultValueHandling.Include)] 
    

    Applying to the class in the original question:

    public class A 
    {
       [JsonProperty(DefaultValueHandling = DefaultValueHandling.Include)]        
       public int P1 { get; internal set; }
    }
    

提交回复
热议问题