JamesNK/Newtonsoft.Json · error · JsonException
Error creating '{0}'.
Error message
Error creating '{0}'. What it means
Catch-all thrown by GetCreator's lambda wrapping any exception raised while invoking the chosen constructor (default or parameterized). '{0}' is the type being created; the real cause is attached as InnerException. This is the outer envelope whenever converter/naming-strategy instantiation fails for any reason other than the no-match/no-default cases (230, 231).
Source
Thrown at Src/Newtonsoft.Json/Serialization/JsonTypeReflector.cs:307
ObjectConstructor<object> parameterizedConstructor = ReflectionDelegateFactory.CreateParameterizedConstructor(parameterizedConstructorInfo);
return parameterizedConstructor(parameters);
}
else
{
throw new JsonException("No matching parameterized constructor found for '{0}'.".FormatWith(CultureInfo.InvariantCulture, type));
}
}
if (defaultConstructor == null)
{
throw new JsonException("No parameterless constructor defined for '{0}'.".FormatWith(CultureInfo.InvariantCulture, type));
}
return defaultConstructor();
}
catch (Exception ex)
{
throw new JsonException("Error creating '{0}'.".FormatWith(CultureInfo.InvariantCulture, type), ex);
}
};
}
#if !(NET20 || DOTNET)
[RequiresUnreferencedCode(MiscellaneousUtils.TrimWarning)]
[RequiresDynamicCode(MiscellaneousUtils.AotWarning)]
private static Type? GetAssociatedMetadataType(Type type)
{
return AssociatedMetadataTypesCache.Instance.Get(type);
}
[RequiresUnreferencedCode(MiscellaneousUtils.TrimWarning)]
[RequiresDynamicCode(MiscellaneousUtils.AotWarning)]
private static Type? GetAssociateMetadataTypeFromAttribute(Type type)
{
Attribute[] customAttributes = ReflectionUtils.GetAttributes(type, null, true);
View on GitHub (pinned to 4f73e74372)
Solutions
- Inspect InnerException to find the constructor's real exception and message.
- Correct the arguments passed via [JsonConverter(typeof(T), ...)] so the constructor does not throw.
- Make the converter constructor tolerate/validate its inputs gracefully, or move validation out of the constructor.
- If the constructor needs runtime services, register a pre-built converter instance via settings.Converters instead of the attribute.
Example fix
// before: ctor throws on bad format
[JsonConverter(typeof(DateFmt), "not-a-format")]
public DateTime When { get; set; }
public DateFmt(string f) { _fmt = DateTime.ParseExact(DateTime.Now.ToString(), f, null); }
// after: ctor stores format, parsing deferred
public DateFmt(string f) { _fmt = f; } Defensive patterns
Strategy: try-catch
Validate before calling
if (type.GetConstructor(args?.Select(a=>a.GetType()).ToArray() ?? Type.EmptyTypes == null ? Type.EmptyTypes : args.Select(a=>a.GetType()).ToArray()) == null) throw new ArgumentException("no matching ctor"); Try / catch
try { var conv = (JsonConverter)Activator.CreateInstance(type, args); }
catch (TargetInvocationException ex) { logger.Error(ex.InnerException, "converter ctor threw"); throw; }
// then for the Json.NET path:
try { JsonConvert.DeserializeObject<T>(json, new JsonSerializerSettings{ Converters = { ... } }); }
catch (JsonException ex) when (ex.Message.Contains("Error creating")) { logger.Error(ex.InnerException, "converter instantiation failed"); throw; } Prevention
- Make converter constructors validate arguments defensively without throwing.
- Keep converter configuration outside the constructor where possible (init methods).
- Register fully-configured converter instances via settings.Converters when constructor needs runtime services.
- Unit-test converter instantiation with realistic argument values.
When it happens
Trigger: The constructor of a JsonConverter or NamingStrategy throws (e.g. ArgumentException for an invalid format string, ArgumentNullException, custom validation throwing), or reflection invocation throws TargetInvocationException wrapping a constructor failure.
Common situations: A converter constructor validates its arguments and throws on bad input (e.g. invalid date format string), a naming-strategy that throws on null arguments, or a converter whose constructor depends on a service that isn't available. Distinct from 230/231 in that a matching constructor exists but threw.
Related errors
- Cannot pass a null parameter to the constructor.
- No matching parameterized constructor found for '{0}'.
- No parameterless constructor defined for '{0}'.
- Could not get constructor for {0}.
- Unable to find default constructor for
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/7f9491bb177beda2.
Report an issue: GitHub.