{"record":{"id":"2c91e111bc082cb0","repo":"JamesNK/Newtonsoft.Json","slug":"the-number-of-elements-in-the-source-jobject-is-gr","errorCode":null,"errorMessage":"The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array.","messagePattern":"The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JContainer.cs","lineNumber":605,"sourceCode":"        }\n\n        internal virtual void CopyItemsTo(Array array, int arrayIndex)\n        {\n            if (array == null)\n            {\n                throw new ArgumentNullException(nameof(array));\n            }\n            if (arrayIndex < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(arrayIndex), \"arrayIndex is less than 0.\");\n            }\n            if (arrayIndex >= array.Length && arrayIndex != 0)\n            {\n                throw new ArgumentException(\"arrayIndex is equal to or greater than the length of array.\");\n            }\n            if (Count > array.Length - arrayIndex)\n            {\n                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.\");\n            }\n\n            int index = 0;\n            foreach (JToken token in ChildrenTokens)\n            {\n                array.SetValue(token, arrayIndex + index);\n                index++;\n            }\n        }\n\n        internal static bool IsTokenUnchanged(JToken currentValue, JToken? newValue)\n        {\n            if (currentValue is JValue v1)\n            {\n                if (newValue == null)\n                {\n                    // null will get turned into a JValue of type null\n                    return v1.Type == JTokenType.Null;","sourceCodeStart":587,"sourceCodeEnd":623,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JContainer.cs#L587-L623","documentation":"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.","triggerScenarios":"Copying a 5-element JArray into a 3-element array; an arrayIndex that leaves too little room.","commonSituations":"Undersized destination array; offset that shrinks available space below Count.","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."],"exampleFix":"// before\nvar dest = new JToken[2];\narr.CopyTo(dest, 0);\n// after\nvar dest = new JToken[arr.Count];\narr.CopyTo(dest, 0);","handlingStrategy":"validation","validationCode":"if (container.Count > dest.Length - arrayIndex) {\n    Array.Resize(ref dest, arrayIndex + container.Count);\n}\ncontainer.CopyTo(dest, arrayIndex);","typeGuard":null,"tryCatchPattern":"try { container.CopyTo(dest, arrayIndex); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"available space\")) {\n    Array.Resize(ref dest, arrayIndex + container.Count);\n    container.CopyTo(dest, arrayIndex);\n}","preventionTips":["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."],"tags":["jcontainer","copyto","bounds","argument"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}