JamesNK/Newtonsoft.Json · error · ArgumentException
The number of elements in the source JObject is greater than
Error message
The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array.
What it means
CopyItemsTo throws ArgumentException when the source container's Count exceeds the available space from arrayIndex to the end of the destination array (Count > array.Length - arrayIndex). The destination must be large enough to hold every child token.
Source
Thrown at Src/Newtonsoft.Json/Linq/JContainer.cs:605
}
internal virtual void CopyItemsTo(Array array, int arrayIndex)
{
if (array == null)
{
throw new ArgumentNullException(nameof(array));
}
if (arrayIndex < 0)
{
throw new ArgumentOutOfRangeException(nameof(arrayIndex), "arrayIndex is less than 0.");
}
if (arrayIndex >= array.Length && arrayIndex != 0)
{
throw new ArgumentException("arrayIndex is equal to or greater than the length of array.");
}
if (Count > array.Length - arrayIndex)
{
throw new ArgumentException("The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array.");
}
int index = 0;
foreach (JToken token in ChildrenTokens)
{
array.SetValue(token, arrayIndex + index);
index++;
}
}
internal static bool IsTokenUnchanged(JToken currentValue, JToken? newValue)
{
if (currentValue is JValue v1)
{
if (newValue == null)
{
// null will get turned into a JValue of type null
return v1.Type == JTokenType.Null;View on GitHub (pinned to 4f73e74372)
Solutions
- Allocate destination as new JToken[container.Count] and copy at index 0.
- When using an offset, size as new JToken[arrayIndex + container.Count].
- Check Count and array.Length - arrayIndex before calling CopyTo.
Example fix
// before var dest = new JToken[2]; arr.CopyTo(dest, 0); // after var dest = new JToken[arr.Count]; arr.CopyTo(dest, 0);
Defensive patterns
Strategy: validation
Validate before calling
if (container.Count > dest.Length - arrayIndex) {
Array.Resize(ref dest, arrayIndex + container.Count);
}
container.CopyTo(dest, arrayIndex); Try / catch
try { container.CopyTo(dest, arrayIndex); }
catch (ArgumentException ex) when (ex.Message.Contains("available space")) {
Array.Resize(ref dest, arrayIndex + container.Count);
container.CopyTo(dest, arrayIndex);
} Prevention
- Allocate dest as new JToken[container.Count] and copy at 0 when possible.
- When using an offset, size as arrayIndex + container.Count.
- Check Count against dest.Length - arrayIndex before CopyTo.
When it happens
Trigger: Copying a 5-element JArray into a 3-element array; an arrayIndex that leaves too little room.
Common situations: Undersized destination array; offset that shrinks available space below Count.
Related errors
- arrayIndex is less than 0.
- arrayIndex is equal to or greater than the length of array.
- Index must be within the bounds of the List.
- Index is less than 0.
- Index is equal to or greater than Count.
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/2c91e111bc082cb0.
Report an issue: GitHub.