By ··7 min read

Newtonsoft.Json drops properties: [DataContract] on a base class

A new API response inherited a base class marked [DataContract], and every property vanished from the JSON. Here is how Newtonsoft.Json picks members, a console repro, and two ways to fix it.

I was adding new API responses to an Optimizely CMS 12 site. Each response class inherited a shared base class that carries an Errors list. The first call to one of them returned only this, and nothing threw an exception:

{"errors":[]}

Every property I had written was missing. Only the one from the base class had survived.

How Newtonsoft decides what to write

Newtonsoft.Json is the long-standing .NET JSON library (also called Json.NET). In an ASP.NET Core app it is switched on with AddNewtonsoftJson(). It builds a “contract” for each type: the list of members it will read and write. The class that builds contracts and picks each member’s JSON name is the contract resolver; CamelCasePropertyNamesContractResolver, for example, turns OrderNumber into orderNumber.

A contract works in one of two modes.

  • Opt-out is the default. Every public property and field is written. You hide one with [JsonIgnore].
  • Opt-in writes only members that say so, with [JsonProperty] or [DataMember]. Everything else is skipped without a warning.

DataContractAttribute (from System.Runtime.Serialization) switches a type to opt-in. The Newtonsoft documentation says it directly: a type marked this way is another way to make member serialisation opt-in, and a member then needs JsonProperty or DataMember to be written.

The part that bit me is where the library looks for the attribute. Here is GetDataContractAttribute in the Newtonsoft source:

JsonTypeReflector.cs (Newtonsoft.Json source)
// DataContractAttribute does not have inheritance
Type? currentType = type;
while (currentType != null)
{
DataContractAttribute? result = CachedAttributeGetter<DataContractAttribute>.GetAttribute(currentType);
if (result != null)
{
return result;
}
currentType = currentType.BaseType();
}

The comment says the attribute itself does not inherit, so the library walks the base types by hand and takes the first one it finds. A child class with no attributes still gets the opt-in contract of its parent.

A decision: if DataContract is on the type or any base class, Newtonsoft writes only DataMember or JsonProperty members, otherwise every public member
Newtonsoft's decision, made once per type

A repro you can run

The whole thing fits in one console app. It targets .NET 8 with Newtonsoft.Json 13.0.4 and the Microsoft.AspNetCore.Mvc.NewtonsoftJson 8.0.32 package. The resolver below is the camelCase one that my site’s responses used.

Program.cs
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
var camel = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
Console.WriteLine(JsonConvert.SerializeObject(new PlainProfile { OrderNumber = "A-1", ItemCount = 3 }, camel));
Console.WriteLine(JsonConvert.SerializeObject(new ProfileResponse { OrderNumber = "A-1", ItemCount = 3 }, camel));
public class PlainProfile { public string OrderNumber { get; set; } = ""; public int ItemCount { get; set; } }
[DataContract]
public class ApiResponse { [DataMember(Name = "errors")] public List<string> Errors { get; set; } = new(); }
public class ProfileResponse : ApiResponse { public string OrderNumber { get; set; } = ""; public int ItemCount { get; set; } }

I ran it with dotnet run on .NET 10. This is the output:

{"orderNumber":"A-1","itemCount":3}
{"errors":[]}

PlainProfile has no attribute anywhere, so both properties come out. ProfileResponse has the same two properties and no attribute of its own, but it inherits from ApiResponse, and the only member written is errors. There was no error and no warning. That is why nobody noticed until a request to the endpoint came back empty.

ProfileResponse inherits ApiResponse. With DataContract on ApiResponse the output is only errors; without it the output includes orderNumber
The child class never mentions the attribute, and it still decides the output

The first fix, and why I backed it out

The quick fix is to opt every member in. I put [DataMember(Name = "...")] on each property of the new responses and [DataContract] on each class: about 160 properties across 30 files. It worked. The repro with that change:

Program.cs
public class ProfileResponseFixed : ApiResponse
{
[DataMember(Name = "orderNumber")] public string OrderNumber { get; set; } = "";
[DataMember(Name = "itemCount")] public int ItemCount { get; set; }
}
{"orderNumber":"A-1","itemCount":3,"errors":[]}

It was also a lot of noise for no gain. The attributes only repeated the name the camelCase resolver already produced. A reviewer on the team questioned exactly that, and they were right.

The final fix: drop the attribute, keep the names

The base class was a response envelope: a shared wrapper around the Errors list. Its [DataContract] was the cause, so I removed the envelope and then removed the attributes from the 30 models. Here is the same repro with a plain base class:

Program.cs
public class PlainApiResponse { public List<string> Errors { get; set; } = new(); }
public class ProfileResponseFinal : PlainApiResponse { public string OrderNumber { get; set; } = ""; public int ItemCount { get; set; } }
{"orderNumber":"A-1","itemCount":3,"errors":[]}

Same JSON as the 160-attribute version. Before deleting anything I wanted proof the names would not change. I serialised all 30 types, without their attributes, through CamelCasePropertyNamesContractResolver and compared the keys with the [DataMember(Name)] values in the original code. Every key matched, except on one type where an earlier commit had renamed fields on purpose.

The same check in the repro uses reflection, which means reading the properties and attributes of a type in code. It compares the resolver’s keys for the plain class with the attribute names on the attributed one:

Program.cs
var resolver = new CamelCasePropertyNamesContractResolver();
var fromResolver = ((JsonObjectContract)resolver.ResolveContract(typeof(ProfileResponseFinal))).Properties
.Select(p => p.PropertyName).OrderBy(x => x);
var fromAttributes = typeof(ProfileResponseFixed).GetProperties()
.Select(p => p.GetCustomAttributes(typeof(DataMemberAttribute), true).Cast<DataMemberAttribute>().FirstOrDefault()?.Name ?? p.Name)
.OrderBy(x => x);
Console.WriteLine($"resolver=[{string.Join(",", fromResolver)}] attributes=[{string.Join(",", fromAttributes)}]");
resolver=[errors,itemCount,orderNumber] attributes=[errors,itemCount,orderNumber]

The attributes do real work somewhere else, though. In the older models the new ones were copied from, one class has four properties whose [DataMember] name the resolver would never produce, such as SkuReference written as productCode. One of them is a misspelling the front end already depends on.

Where a name is not just the camelCase of the property, put [JsonProperty("productCode")] on that one property. [DataMember(Name = ...)] alone does nothing there, because Newtonsoft reads it only on a [DataContract] type.

The order of the two removals matters. If I had stripped the attributes from the models while the [DataContract] base class was still there, the models would have gone back to {"errors":[]}. Remove the cause first, then the attributes.

A side note on casing

I believed at first that AddNewtonsoftJson() leaves property names in PascalCase, which is what plain JsonConvert does. I nearly put that in my reply to the review. Testing showed it was wrong. The default settings come from JsonSerializerSettingsProvider in the ASP.NET Core source, which registers a DefaultContractResolver with a CamelCaseNamingStrategy. The repro builds those settings the way an app does and prints them:

AddNewtonsoftJson resolver: DefaultContractResolver, naming: CamelCaseNamingStrategy
default output {"orderNumber":"A-1","itemCount":3}
bare DefaultContractResolver {"OrderNumber":"A-1","ItemCount":3}

So in the original project, the framework’s own output would have been camelCase too. Request bodies bound either way, because Newtonsoft matches property names without regard to case when it reads JSON. A bare new JsonSerializerSettings() keeps PascalCase, so settings the app builds itself, as my site’s helper did, need their own camelCase resolver.

Which serialiser reads which attribute

The reason this is confusing is that three serialisers share the same attribute names and treat them differently. The third is DataContractSerializer, the older XML serialiser that the System.Runtime.Serialization attributes were made for. This is from the documentation and source for each, checked in October 2026.

Attribute Newtonsoft.Json 13 System.Text.Json (.NET 8 to 10) DataContractSerializer
[DataContract] on the type Makes it opt-in, also from a base class No built-in support Marks the type as an explicit contract
[DataMember] Includes the member, and Name renames it, only on a [DataContract] type No built-in support Includes the member in a marked type
[IgnoreDataMember] Skips the member on an opt-out type No built-in support Honoured only on types without [DataContract] or [Serializable]
[JsonIgnore] Its own [JsonIgnore] skips the member Its own [JsonIgnore] skips the member Not applicable
[JsonProperty] / [JsonPropertyName] [JsonProperty] renames or opts in [JsonPropertyName] renames Not applicable

The two [JsonIgnore] attributes are different classes in different namespaces, and each serialiser reads only its own. System.Text.Json, the serialiser built into .NET, does not read the System.Runtime.Serialization attributes at all. Since .NET 7 you can add support with a custom type resolver. That means a model with [DataContract] serialises one way under Newtonsoft and another under System.Text.Json, which is a good reason to keep the attributes off models that more than one serialiser touches.

How to check it worked

  1. Call the endpoint and confirm the body holds the properties, not only errors.
  2. Paste the repro’s Console.WriteLine lines into a throwaway console app that references your own response types, and print them with your own settings.
  3. Search the project for [DataContract] and look at the base classes of every response type.

Takeaways

  • Newtonsoft writes every public member unless a type, or any base class, has [DataContract]. Then it writes only [DataMember] or [JsonProperty] members, silently.
  • A response that holds only {"errors":[]}, with no exception, is the symptom to remember.
  • An attribute on a base class changes every child. Check inheritance before blaming the child.
  • Drop attributes that only repeat the resolver’s naming, and prove it by comparing keys through reflection.
  • Keep [JsonProperty] on the few properties whose JSON name is not the camelCase of the property.
  • Remove the cause (the base-class attribute) before the attributes it forced you to add.
  • Print what AddNewtonsoftJson() and your own helpers actually produce for casing.