JamesNK/Newtonsoft.Json · error · InvalidOperationException
Unable to find default constructor for
Error message
Unable to find default constructor for
What it means
Thrown by LateBoundReflectionDelegateFactory.CreateDefaultConstructor when ReflectionUtils.GetDefaultConstructor(type, true) returns null for a reference type. GetDefaultConstructor is called with nonPublic=true, so this means there is genuinely no parameterless constructor (public or non-public) on the type. The LateBound factory is the fallback path used for structs, abstract types, and WinRT/Win8 edge cases, or when the Expression factory is unavailable.
Source
Thrown at Src/Newtonsoft.Json/Utilities/LateBoundReflectionDelegateFactory.cs:86
return (o, a) => method.Invoke(o, a);
}
[RequiresDynamicCode(MiscellaneousUtils.AotWarning)]
public override Func<T> CreateDefaultConstructor<T>(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
Type type)
{
ValidationUtils.ArgumentNotNull(type, nameof(type));
if (type.IsValueType())
{
return () => (T)Activator.CreateInstance(type)!;
}
ConstructorInfo? constructorInfo = ReflectionUtils.GetDefaultConstructor(type, true);
if (constructorInfo == null)
{
throw new InvalidOperationException("Unable to find default constructor for " + type.FullName);
}
return () => (T)constructorInfo.Invoke(null);
}
[RequiresDynamicCode(MiscellaneousUtils.AotWarning)]
public override Func<T, object?> CreateGet<T>(PropertyInfo propertyInfo)
{
ValidationUtils.ArgumentNotNull(propertyInfo, nameof(propertyInfo));
return o => propertyInfo.GetValue(o, null);
}
[RequiresDynamicCode(MiscellaneousUtils.AotWarning)]
public override Func<T, object?> CreateGet<T>(FieldInfo fieldInfo)
{
ValidationUtils.ArgumentNotNull(fieldInfo, nameof(fieldInfo));
View on GitHub (pinned to 4f73e74372)
Solutions
- Add a parameterless constructor to the type (protected/private is fine since nonPublic=true is passed).
- Mark one parameterized constructor with [JsonConstructor] so Json.NET uses it instead of seeking a default ctor.
- Use a custom IContractResolver overriding CreateObjectContract to set an OverrideCreator with the desired constructor.
- If using ReflectionObject directly, pass an explicit creator MethodBase to ReflectionObject.Create.
Example fix
// before
public class Money
{
public Money(decimal amount, string currency) { ... }
}
// after
public class Money
{
public Money(decimal amount, string currency) { ... }
[JsonConstructor]
public Money() { Amount = 0; Currency = "USD"; }
} Defensive patterns
Strategy: validation
Validate before calling
// Verify a parameterless ctor exists before serializing/deserializing.
bool HasDefaultCtor(Type t) =>
t.IsValueType || t.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null) != null; Prevention
- Always provide a parameterless constructor on serialized DTOs, or mark one parameterized ctor with [JsonConstructor].
- Add a unit test that deserializes a sample JSON for each DTO to catch missing-ctor issues at build time.
- For third-party types, wrap them in a serializable adapter or use a custom IContractResolver.
When it happens
Trigger: Deserializing into a class that only declares parameterized constructors and has no parameterless one — and no [JsonConstructor] marks a parameterized constructor. Also triggered by ReflectionObject.Create when no creator method is supplied and the type lacks a default constructor.
Common situations: DTOs with only parameterized constructors (DDD-style or record types), types designed for DI containers that never need a default ctor, removing an accidental parameterless ctor during refactoring, or third-party types you cannot modify.
Related errors
- Multiple constructors with the JsonConstructorAttribute.
- Error setting value to '{0}' on '{1}'.
- Error setting value to '{0}' on '{1}'.
- Cannot pass a null parameter to the constructor.
- No matching parameterized constructor found for '{0}'.
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/147800c1bfe28b4b.
Report an issue: GitHub.