JamesNK/Newtonsoft.Json · error · ArgumentException

Constructor name cannot be empty.

Error message

Constructor name cannot be empty.

What it means

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.

Source

Thrown at Src/Newtonsoft.Json/Linq/JConstructor.cs:146

            : this(name)
        {
            Add(content);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="JConstructor"/> class with the specified name.
        /// </summary>
        /// <param name="name">The constructor name.</param>
        public JConstructor(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (name.Length == 0)
            {
                throw new ArgumentException("Constructor name cannot be empty.", nameof(name));
            }

            _name = name;
        }

        internal override bool DeepEquals(JToken node)
        {
            return (node is JConstructor c && _name == c.Name && ContentsEqual(c));
        }

        internal override JToken CloneToken(JsonCloneSettings? settings = null)
        {
            return new JConstructor(this, settings);
        }

        /// <summary>
        /// Writes this token to a <see cref="JsonWriter"/>.
        /// </summary>

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Validate the name is non-empty before constructing, and surface a clearer error to the caller.
  2. Default to a placeholder identifier when the source name is missing.
  3. Skip construction entirely when the name is null or empty.

Example fix

// before
var c = new JConstructor(name ?? "");
// after
if (string.IsNullOrEmpty(name)) throw new ArgumentException("name required", nameof(name));
var c = new JConstructor(name);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(name)) throw new ArgumentException("Constructor name required.", nameof(name));
var c = new JConstructor(name);

Try / catch

try { var c = new JConstructor(name); }
catch (ArgumentException ex) when (ex.ParamName == "name") {
    // supply a default name or report upstream
}

Prevention

When it happens

Trigger: new JConstructor(string.Empty); constructing from a name variable that resolved to an empty string.

Common situations: Names read from a database/config field that allow empty; trimming user input to empty; deserializing malformed constructor tokens.

Related errors


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