title: Delphi TJSONArray.ToBytes Access violation - Is it
body: <p>The problem is at the TJSONArray.ToBytes call at the end. The target TBytes repository exists. Have I set the size incorrectly using SizeOf? TJSONArray.Count must surely give only the number of objects in the JSON array. What did I miss? I tried an offset of 1 for the second argument to ToBytes, but it did not work and I do not think that it is the correct index... </p>
  
  <p>The code successfully reads JSON from a file into iCJSONCallData (a TJSONArray object).</p>
  
  <pre><code>  // Private variables in the class
      iCJSONAsStr          : string;
      iCJSONBytes          : TBytes;
      iCJSONAllSourceBytes : TBytes;
      iCJSONByteStream     : TBytesStream;
      iCCallData   : TStringList;
      iCJSONCallData  : TJSONArray;
      iCJSONCallSet   : TJSONValue;
      iCJSONParamData : TJSONArray;
  .
  .
  .
      var
        vJSONCalls              : TJSONArray;
        vJSONCallElement        : TJSONObject;
        vJSONCallPair           : TJSONPair;
        vJSONCallEntry          : TJSONString;
        vJSONCallValue          : TJSONValue;
        vJSONCAllDataBytes : TBytes;
        vJSONCallPairEnumerator : TJSONPairEnumerator;
        vJSONCallList           : TStringList;
        vJSONCallDesc           : string;
        vPathCount              : integer;
        vCount : integer;
        vCurrentFileName        : string;
  
  
      begin
  
         if iCJSONByteStream = nil then
          iCJSONByteStream := TBytesStream.Create;
  
        if vJSONCallList = nil then
          vJSONCallList := TStringList.Create;
  
        for vCurrentFileName in uCRPCDataFileNames do 
        begin
  
          iCJSONBytes := TFile.ReadAllBytes(vCurrentFileName);
          iCJSONAsStr := TFile.ReadAllText(vCurrentFileName);
  
          if iCCallData = nil then
            iCCallData := TStringList.Create;
          if iCJSONCallData = nil then
            iCJSONCallData := TJSONArray.Create();
          if iCJSONCallSet = nil then
            iCJSONCallSet := TJSONValue.Create;
  
          iCJSONCallSet := TJSONObject.ParseJSONValue(iCJSONBytes , 0);
  
          if iCJSONCallSet &lt;&gt; nil then
            try
              iCJSONCallData := iCJSONCallSet as TJSONArray;
  
              try
                for vJSONCallValue in iCJSONCallData do
                begin
                  vJSONCallElement := vJSONCallValue as TJSONObject;
                  vJSONCallPair    := vJSONCallElement.Get(uCCallKey); //uCCallKey is a string
  
                  vJSONCallEntry := vJSONCallPair.JsonString;
  
                  vJSONCallDesc := vJSONCallPair.JsonValue.ToString;
  
                  iCCallData.Append(vJSONCallDesc);
                end;
  
              except
                on E : Exception do
                ShowMessage('Error ' + E.Message);
              end;
  
              SetLength(vJSONCAllDataBytes, SizeOf(iCJSONCallData));
              vCount := iCJSONCallData.ToBytes(vJSONCAllDataBytes, 0); //Access violation... &lt;&lt;&lt;==
  
            finally
  
  
            end;
  </code></pre>