TMaddox
TMaddox

Reputation: 87

Ignore all fields and properties by default

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.

Upvotes: 1

Views: 283

Answers (1)

andyb952
andyb952

Reputation: 2008

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

Upvotes: 2

Related Questions