JamesNK/Newtonsoft.Json · error · JsonException
Constructor for '{0}' must have no parameters or a single pa
Error message
Constructor for '{0}' must have no parameters or a single parameter that implements '{1}'. What it means
When building a JsonObjectContract for a dictionary type whose constructor is marked [JsonConstructor], that constructor must be parameterless or take exactly one parameter assignable to the dictionary's expected collection type (IDictionary, or IEnumerable<KeyValuePair<TKey,TValue>>). Any other signature throws a JsonException (line 1047). The deserializer needs to construct the dictionary from an existing collection, so the parameter shape is constrained.
Source
Thrown at Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs:1047
if (overrideConstructor != null)
{
ParameterInfo[] parameters = overrideConstructor.GetParameters();
Type expectedParameterType = (contract.DictionaryKeyType != null && contract.DictionaryValueType != null)
? typeof(IEnumerable<>).MakeGenericType(typeof(KeyValuePair<,>).MakeGenericType(contract.DictionaryKeyType, contract.DictionaryValueType))
: typeof(IDictionary);
if (parameters.Length == 0)
{
contract.HasParameterizedCreator = false;
}
else if (parameters.Length == 1 && expectedParameterType.IsAssignableFrom(parameters[0].ParameterType))
{
contract.HasParameterizedCreator = true;
}
else
{
throw new JsonException("Constructor for '{0}' must have no parameters or a single parameter that implements '{1}'.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType, expectedParameterType));
}
contract.OverrideCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateParameterizedConstructor(overrideConstructor);
}
return contract;
}
/// <summary>
/// Creates a <see cref="JsonArrayContract"/> for the given type.
/// </summary>
/// <param name="objectType">Type of the object.</param>
/// <returns>A <see cref="JsonArrayContract"/> for the given type.</returns>
protected virtual JsonArrayContract CreateArrayContract(Type objectType)
{
JsonArrayContract contract = new JsonArrayContract(objectType);
InitializeContract(contract);
View on GitHub (pinned to 4f73e74372)
Solutions
- Make the [JsonConstructor] constructor parameterless, or give it a single parameter of type IDictionary / IDictionary<TKey,TValue> / IEnumerable<KeyValuePair<TKey,TValue>>.
- Remove [JsonConstructor] so Json.NET uses the default (parameterless) constructor.
Example fix
// before
public class StringMap : Dictionary<string, string> {
[JsonConstructor] public StringMap(int capacity) : base(capacity) {} // throws
}
// after
public class StringMap : Dictionary<string, string> {
public StringMap() {}
[JsonConstructor] public StringMap(IDictionary<string, string> source) : base(source) {}
} Defensive patterns
Strategy: validation
Validate before calling
var ctor = typeof(TDictionary).GetConstructors()
.FirstOrDefault(c => c.IsDefined(typeof(JsonConstructorAttribute), true));
if (ctor != null)
{
var ps = ctor.GetParameters();
var ok = ps.Length == 0
|| (ps.Length == 1 && (typeof(IDictionary).IsAssignableFrom(ps[0].ParameterType)
|| ps[0].ParameterType.GetInterfaces().Any(i => i.IsGenericType
&& i.GetGenericTypeDefinition() == typeof(IEnumerable<>)
&& i.GetGenericArguments()[0].GetGenericTypeDefinition() == typeof(KeyValuePair<,>))));
if (!ok) throw new InvalidOperationException("Dictionary [JsonConstructor] has invalid signature.");
} Type guard
static bool DictionaryConstructorIsValid(Type dictType, out string error)
{
error = string.Empty;
var ctor = dictType.GetConstructors().FirstOrDefault(c => c.IsDefined(typeof(JsonConstructorAttribute), true));
if (ctor == null) return true;
var ps = ctor.GetParameters();
return ps.Length == 0 || ps.Length == 1;
} Try / catch
try { JsonConvert.DeserializeObject<MyDict>(json); }
catch (JsonException ex) when (ex.Message.Contains("must have no parameters or a single parameter"))
{
// adjust the [JsonConstructor] constructor signature for the dictionary
} Prevention
- Give dictionary [JsonConstructor] constructors a parameterless or single-collection-parameter signature.
- Prefer a parameterless constructor for dictionary subclasses when in doubt.
When it happens
Trigger: A Dictionary<TKey,TValue> subclass with [JsonConstructor] on a constructor taking unrelated parameters (e.g. an int capacity or a string).
Common situations: Subclassing a dictionary to add behavior and tagging a non-standard constructor for deserialization.
Related errors
- Multiple constructors with the JsonConstructorAttribute.
- Invalid extension data attribute on '{0}'. Member '{1}' must
- Invalid extension data attribute on '{0}'. Member '{1}' type
- Invalid attribute. Both '{0}' and '{1}' in type '{2}' have '
- Invalid Callback. Method '{3}' in type '{2}' has both '{0}'
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/78a04520ba6b01c9.
Report an issue: GitHub.