{"record":{"id":"35e62ad4a250cdd9","repo":"JamesNK/Newtonsoft.Json","slug":"constructor-name-cannot-be-empty","errorCode":null,"errorMessage":"Constructor name cannot be empty.","messagePattern":"Constructor name cannot be empty\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JConstructor.cs","lineNumber":146,"sourceCode":"            : this(name)\n        {\n            Add(content);\n        }\n\n        /// <summary>\n        /// Initializes a new instance of the <see cref=\"JConstructor\"/> class with the specified name.\n        /// </summary>\n        /// <param name=\"name\">The constructor name.</param>\n        public JConstructor(string name)\n        {\n            if (name == null)\n            {\n                throw new ArgumentNullException(nameof(name));\n            }\n\n            if (name.Length == 0)\n            {\n                throw new ArgumentException(\"Constructor name cannot be empty.\", nameof(name));\n            }\n\n            _name = name;\n        }\n\n        internal override bool DeepEquals(JToken node)\n        {\n            return (node is JConstructor c && _name == c.Name && ContentsEqual(c));\n        }\n\n        internal override JToken CloneToken(JsonCloneSettings? settings = null)\n        {\n            return new JConstructor(this, settings);\n        }\n\n        /// <summary>\n        /// Writes this token to a <see cref=\"JsonWriter\"/>.\n        /// </summary>","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JConstructor.cs#L128-L164","documentation":"The JConstructor(string name) constructor rejects an empty name string with ArgumentException (a null name throws ArgumentNullException separately at line 141). A JSON constructor must have a non-empty identifier.","triggerScenarios":"new JConstructor(string.Empty); constructing from a name variable that resolved to an empty string.","commonSituations":"Names read from a database/config field that allow empty; trimming user input to empty; deserializing malformed constructor tokens.","solutions":["Validate the name is non-empty before constructing, and surface a clearer error to the caller.","Default to a placeholder identifier when the source name is missing.","Skip construction entirely when the name is null or empty."],"exampleFix":"// before\nvar c = new JConstructor(name ?? \"\");\n// after\nif (string.IsNullOrEmpty(name)) throw new ArgumentException(\"name required\", nameof(name));\nvar c = new JConstructor(name);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(name)) throw new ArgumentException(\"Constructor name required.\", nameof(name));\nvar c = new JConstructor(name);","typeGuard":null,"tryCatchPattern":"try { var c = new JConstructor(name); }\ncatch (ArgumentException ex) when (ex.ParamName == \"name\") {\n    // supply a default name or report upstream\n}","preventionTips":["Validate names are non-empty at the source (config/DTO).","Centralize JConstructor creation behind a helper that guards the name.","Trim then check for empty before construction."],"tags":["jconstructor","argument","validation"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}