{"record":{"id":"78a04520ba6b01c9","repo":"JamesNK/Newtonsoft.Json","slug":"constructor-for-0-must-have-no-parameters-or-a","errorCode":null,"errorMessage":"Constructor for '{0}' must have no parameters or a single parameter that implements '{1}'.","messagePattern":"Constructor for '(.+?)' must have no parameters or a single parameter that implements '(.+?)'\\.","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs","lineNumber":1047,"sourceCode":"\n            if (overrideConstructor != null)\n            {\n                ParameterInfo[] parameters = overrideConstructor.GetParameters();\n                Type expectedParameterType = (contract.DictionaryKeyType != null && contract.DictionaryValueType != null)\n                    ? typeof(IEnumerable<>).MakeGenericType(typeof(KeyValuePair<,>).MakeGenericType(contract.DictionaryKeyType, contract.DictionaryValueType))\n                    : typeof(IDictionary);\n\n                if (parameters.Length == 0)\n                {\n                    contract.HasParameterizedCreator = false;\n                }\n                else if (parameters.Length == 1 && expectedParameterType.IsAssignableFrom(parameters[0].ParameterType))\n                {\n                    contract.HasParameterizedCreator = true;\n                }\n                else\n                {\n                    throw new JsonException(\"Constructor for '{0}' must have no parameters or a single parameter that implements '{1}'.\".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType, expectedParameterType));\n                }\n\n                contract.OverrideCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateParameterizedConstructor(overrideConstructor);\n            }\n\n            return contract;\n        }\n\n        /// <summary>\n        /// Creates a <see cref=\"JsonArrayContract\"/> for the given type.\n        /// </summary>\n        /// <param name=\"objectType\">Type of the object.</param>\n        /// <returns>A <see cref=\"JsonArrayContract\"/> for the given type.</returns>\n        protected virtual JsonArrayContract CreateArrayContract(Type objectType)\n        {\n            JsonArrayContract contract = new JsonArrayContract(objectType);\n            InitializeContract(contract);\n","sourceCodeStart":1029,"sourceCodeEnd":1065,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs#L1029-L1065","documentation":"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.","triggerScenarios":"A Dictionary<TKey,TValue> subclass with [JsonConstructor] on a constructor taking unrelated parameters (e.g. an int capacity or a string).","commonSituations":"Subclassing a dictionary to add behavior and tagging a non-standard constructor for deserialization.","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."],"exampleFix":"// before\npublic class StringMap : Dictionary<string, string> {\n    [JsonConstructor] public StringMap(int capacity) : base(capacity) {} // throws\n}\n\n// after\npublic class StringMap : Dictionary<string, string> {\n    public StringMap() {}\n    [JsonConstructor] public StringMap(IDictionary<string, string> source) : base(source) {}\n}","handlingStrategy":"validation","validationCode":"var ctor = typeof(TDictionary).GetConstructors()\n    .FirstOrDefault(c => c.IsDefined(typeof(JsonConstructorAttribute), true));\nif (ctor != null)\n{\n    var ps = ctor.GetParameters();\n    var ok = ps.Length == 0\n        || (ps.Length == 1 && (typeof(IDictionary).IsAssignableFrom(ps[0].ParameterType)\n            || ps[0].ParameterType.GetInterfaces().Any(i => i.IsGenericType\n                && i.GetGenericTypeDefinition() == typeof(IEnumerable<>)\n                && i.GetGenericArguments()[0].GetGenericTypeDefinition() == typeof(KeyValuePair<,>))));\n    if (!ok) throw new InvalidOperationException(\"Dictionary [JsonConstructor] has invalid signature.\");\n}","typeGuard":"static bool DictionaryConstructorIsValid(Type dictType, out string error)\n{\n    error = string.Empty;\n    var ctor = dictType.GetConstructors().FirstOrDefault(c => c.IsDefined(typeof(JsonConstructorAttribute), true));\n    if (ctor == null) return true;\n    var ps = ctor.GetParameters();\n    return ps.Length == 0 || ps.Length == 1;\n}","tryCatchPattern":"try { JsonConvert.DeserializeObject<MyDict>(json); }\ncatch (JsonException ex) when (ex.Message.Contains(\"must have no parameters or a single parameter\"))\n{\n    // adjust the [JsonConstructor] constructor signature for the dictionary\n}","preventionTips":["Give dictionary [JsonConstructor] constructors a parameterless or single-collection-parameter signature.","Prefer a parameterless constructor for dictionary subclasses when in doubt."],"tags":["dictionary","constructor","attribute-validation","contract-resolution"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}