1

I am using Newtonsoft.Json to serialize my objects. I want that by default no field or property gets serialized, only if I decorate it with a [JsonProperty(PropertyName = "name")] Attribute. I couldn't find anything in the newtonsoft docs.

TMaddox
  • 77
  • 1
  • 11

1 Answers1

2

You can add [JsonObject(MemberSerialization.OptIn)] attribute to your class, everything will be ignored unless you explicitly Opt-In by using a [JsonProperty] attribute.

[JsonObject(MemberSerialization.OptIn)]
public class MyClass
{
    [JsonProperty]
    public string NotIgnored { get; set; }

    public string Line2 { get; set; }

    public string Line3 { get; set; }
}

More info here: Newtonsoft Documentation

andyb952
  • 1,931
  • 11
  • 25
  • works, but it would be better if I could add an option to a JsonSerializerSettings object. Is this possible? – TMaddox Aug 27 '19 at 15:33
  • You can create a contract resolver if you wish, I'm assuming you are asking because you don't want to go back and mark all of your classes with this attribute? – andyb952 Aug 27 '19 at 15:42