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

  1. Add a parameterless constructor to the type (protected/private is fine since nonPublic=true is passed).
  2. Mark one parameterized constructor with [JsonConstructor] so Json.NET uses it instead of seeking a default ctor.
  3. Use a custom IContractResolver overriding CreateObjectContract to set an OverrideCreator with the desired constructor.
  4. 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

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


AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07). Data as JSON: /api/errors/147800c1bfe28b4b. Report an issue: GitHub.