{"record":{"id":"255edadd5d34e8a2","repo":"JamesNK/Newtonsoft.Json","slug":"multiple-constructors-with-the-jsonconstructorattr","errorCode":null,"errorMessage":"Multiple constructors with the JsonConstructorAttribute.","messagePattern":"Multiple constructors with the JsonConstructorAttribute\\.","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs","lineNumber":617,"sourceCode":"                }\n            }\n\n            IEnumerator IEnumerable.GetEnumerator()\n            {\n                return GetEnumerator();\n            }\n        }\n\n        private ConstructorInfo? GetAttributeConstructor(Type objectType)\n        {\n            IEnumerator<ConstructorInfo> en = objectType.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(c => c.IsDefined(typeof(JsonConstructorAttribute), true)).GetEnumerator();\n\n            if (en.MoveNext())\n            {\n                ConstructorInfo conInfo = en.Current;\n                if (en.MoveNext())\n                {\n                    throw new JsonException(\"Multiple constructors with the JsonConstructorAttribute.\");\n                }\n\n                return conInfo;\n            }\n\n            // little hack to get Version objects to deserialize correctly\n            if (objectType == typeof(Version))\n            {\n                return objectType.GetConstructor(new[] { typeof(int), typeof(int), typeof(int), typeof(int) });\n            }\n\n            return null;\n        }\n\n        private ConstructorInfo? GetImmutableConstructor(Type objectType, JsonPropertyCollection memberProperties)\n        {\n            IEnumerable<ConstructorInfo> constructors = objectType.GetConstructors();\n            IEnumerator<ConstructorInfo> en = constructors.GetEnumerator();","sourceCodeStart":599,"sourceCodeEnd":635,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs#L599-L635","documentation":"Only one constructor per type may carry [JsonConstructor]. GetAttributeConstructor enumerates constructors marked with JsonConstructorAttribute and throws a JsonException (line 617) if a second one is found. The attribute tells Json.NET which constructor to use for deserialization, so an ambiguous choice is rejected at contract build time.","triggerScenarios":"Decorating two (or more) constructors of the same class with [JsonConstructor], e.g. both a parameterized and a default constructor.","commonSituations":"Adding [JsonConstructor] to a new constructor while forgetting to remove it from the old one, or merging two classes during refactoring.","solutions":["Remove [JsonConstructor] from all but one constructor.","If you need a specific deserialization path, keep the single most appropriate constructor and restructure the others without the attribute."],"exampleFix":"// before\npublic class Widget {\n    [JsonConstructor] public Widget(int a) {}\n    [JsonConstructor] public Widget(int a, int b) {} // throws\n}\n\n// after\npublic class Widget {\n    public Widget(int a) {}\n    [JsonConstructor] public Widget(int a, int b) {}\n}","handlingStrategy":"validation","validationCode":"// Fail fast in a unit test if more than one constructor is marked [JsonConstructor].\nvar tagged = typeof(T).GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)\n    .Where(c => c.IsDefined(typeof(JsonConstructorAttribute), true)).ToList();\nif (tagged.Count > 1)\n    throw new InvalidOperationException(\"Multiple [JsonConstructor] constructors on \" + typeof(T));","typeGuard":"static bool HasSingleJsonConstructor(Type t) =>\n    t.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)\n     .Count(c => c.IsDefined(typeof(JsonConstructorAttribute), true)) == 1;","tryCatchPattern":"try { JsonConvert.DeserializeObject<T>(json); }\ncatch (JsonException ex) when (ex.Message.Contains(\"Multiple constructors\"))\n{\n    // remove [JsonConstructor] from all but one constructor\n}","preventionTips":["Tag at most one constructor with [JsonConstructor].","Add a reflection-based test that asserts the invariant for serializable types."],"tags":["constructor","attribute-validation","deserialization","contract-resolution"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}