JamesNK/Newtonsoft.Json · error · ArgumentException
Must specify valid information for parsing in the string.
Error message
Must specify valid information for parsing in the string.
What it means
ParseEnum (EnumUtils.cs:281-293) scans the input string for the first non-whitespace character to begin parsing. If the entire string is whitespace (firstNonWhitespaceIndex stays -1), it throws ArgumentException at EnumUtils.cs:292 — there is no content to parse into an enum value.
Source
Thrown at Src/Newtonsoft.Json/Utilities/EnumUtils.cs:292
// first check if the entire text (including commas) matches a resolved name
int? matchingIndex = FindIndexByName(resolvedNames, value, 0, value.Length, StringComparison.Ordinal);
if (matchingIndex != null)
{
return Enum.ToObject(enumType, enumValues[matchingIndex.Value]);
}
int firstNonWhitespaceIndex = -1;
for (int i = 0; i < value.Length; i++)
{
if (!char.IsWhiteSpace(value[i]))
{
firstNonWhitespaceIndex = i;
break;
}
}
if (firstNonWhitespaceIndex == -1)
{
throw new ArgumentException("Must specify valid information for parsing in the string.");
}
// check whether string is a number and parse as a number value
char firstNonWhitespaceChar = value[firstNonWhitespaceIndex];
if (char.IsDigit(firstNonWhitespaceChar) || firstNonWhitespaceChar == '-' || firstNonWhitespaceChar == '+')
{
Type underlyingType = Enum.GetUnderlyingType(enumType);
value = value.Trim();
object? temp = null;
try
{
temp = Convert.ChangeType(value, underlyingType, CultureInfo.InvariantCulture);
}
catch (FormatException)
{
// We need to Parse this as a String instead. There are casesView on GitHub (pinned to 4f73e74372)
Solutions
- Validate the string with !string.IsNullOrWhiteSpace(value) before parsing.
- Treat blank/empty as the enum's default value (or a designated 'Unknown' member) instead of calling ParseEnum.
- Sanitize incoming JSON/string values at the boundary.
- Use StringEnumConverter with a fallback for null/empty.
Example fix
// before var e = (MyEnum)Enum.Parse(typeof(MyEnum), s); // s = " " -> throws // after if (string.IsNullOrWhiteSpace(s)) return default(MyEnum); var e = (MyEnum)Enum.Parse(typeof(MyEnum), s.Trim());
Defensive patterns
Strategy: validation
Validate before calling
// Reject blank strings before parsing an enum.
if (string.IsNullOrWhiteSpace(value)) throw new ArgumentException("Enum value must not be blank.");
value = value.Trim(); Type guard
static bool IsParsableEnumString(string? s) => !string.IsNullOrWhiteSpace(s);
Try / catch
try { return EnumUtils.ParseEnum(enumType, null, value, false); } catch (ArgumentException ex) when (ex.Message.Contains("valid information for parsing")) { return default; } Prevention
- Validate enum strings with !IsNullOrWhiteSpace before parsing.
- Map blank/empty to a designated default member.
- Sanitize incoming JSON values at the boundary.
When it happens
Trigger: An empty or all-whitespace string is supplied as an enum value — e.g. JSON `"status": " "` for an enum field, or a value that was trimmed to empty.
Common situations: Whitespace-only enum strings in JSON payloads; upstream trimming producing empty values; default/missing fields represented as blank strings; integration with APIs that emit empty for unset.
Related errors
- Unexpected merge array handling when merging JSON.
- value
- value
- Enum name '{0}' already exists on enum '{1}'.
- Enum type {0} is not a set of flags.
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/7f8fd2056fb4b1d9.
Report an issue: GitHub.