In order to make a property deserialize but not serialize with JSON.NET, you can use the [JsonProperty] attribute with JsonPropertyAttribute.PropertyName and JsonSerializerSettings with NullValueHandling to ignore null values during serialization.
Here is an example:
public class MyClass { // property to be deserialized but not serialized [JsonProperty(PropertyName = "myProperty", DefaultValueHandling = DefaultValueHandling.Populate)] public int MyProperty { get; set; } = 0; // property to be serialized normally public string MyOtherProperty { get; set; } } // deserialization var jsonString = "{\"myProperty\": 42, \"myOtherProperty\": \"hello\"}"; var myObject = JsonConvert.DeserializeObject<MyClass>(jsonString); // serialization var settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; var jsonString = JsonConvert.SerializeObject(myObject, settings); In this example, the MyClass has two properties: MyProperty and MyOtherProperty. MyProperty is annotated with the [JsonProperty] attribute with JsonPropertyAttribute.PropertyName = "myProperty" and DefaultValueHandling.Populate. This means that it will be deserialized but not serialized. MyOtherProperty is not annotated and will be serialized and deserialized normally.
During serialization, we create a JsonSerializerSettings object and set its NullValueHandling property to NullValueHandling.Ignore so that any properties with a null value are ignored. We then serialize the object using the JsonConvert.SerializeObject method and passing in the object and the settings.
Note that the JsonPropertyAttribute.DefaultValueHandling property is set to DefaultValueHandling.Populate. This is important because it ensures that the default value of MyProperty is set to 0 if it is not present in the JSON string.
"Json.NET deserialize-only property C# example"
public class MyClass { [JsonProperty(PropertyName = "myProperty", DefaultValueHandling = DefaultValueHandling.Ignore)] public string MyProperty { get; set; } } "Json.NET ignore serialization for specific property C#"
public class MyClass { [JsonProperty(PropertyName = "myProperty", DefaultValueHandling = DefaultValueHandling.Ignore)] public string MyProperty { get; set; } } "Json.NET deserialize-only attribute for C# property"
public class MyClass { [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public string MyProperty { get; set; } } "C# Json.NET deserialize but not serialize property"
public class MyClass { [JsonProperty(PropertyName = "myProperty", DefaultValueHandling = DefaultValueHandling.Ignore)] public string MyProperty { get; set; } } "Json.NET ignore serialization specific property C# code"
public class MyClass { [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public string MyProperty { get; set; } } "C# Json.NET deserialize-only property example"
public class MyClass { [JsonIgnore(Condition = JsonIgnoreCondition.Never)] public string MyProperty { get; set; } } "Json.NET exclude property from serialization in C#"
public class MyClass { [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public string MyProperty { get; set; } } "C# Json.NET ignore serialization but allow deserialization"
public class MyClass { [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public string MyProperty { get; set; } } "Json.NET C# property deserialize-only attribute"
public class MyClass { [JsonIgnore(Condition = JsonIgnoreCondition.Never)] public string MyProperty { get; set; } } "C# Json.NET exclude property from serialization example"
public class MyClass { [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public string MyProperty { get; set; } } sqldf asp.net-core-1.1 sys-refcursor python-dataclasses serverxmlhttp enumeration dojo-1.6 awt raspbian dataformat