[fpc-pascal] Re: JSON & me - blind again?

Reinier Olislagers reinierolislagers at gmail.com
Tue Mar 12 12:04:22 CET 2013


On 12-3-2013 11:44, Michael Van Canneyt wrote:

Thanks for the help, Michael.

> On Tue, 12 Mar 2013, Reinier Olislagers wrote:
>> //===> the line below works - so CommJSON is a valid object, right?
>>        if Assigned(CommJSON) then
> 
> It is assigned, but not necessarily valid. If you freed CommJSON but did
> not nil it, it is still'assigned'.
Understood. Then it has a pointer pointing to whatever (probably the
object's former memory location).

>> I'm not confident about
>> function HttpRequestWithData(AData: TJSONData; const AUrl: string;
>>  const AMethod: TRequestMethod; const AContentType: string): THttpResult;
> 
> Ehm. This must be
> 
>  function HttpRequestWithData(OUT AData: TJSONData; const AUrl: string;
>   const AMethod: TRequestMethod; const AContentType: string): THttpResult;
> 
> Because you are passing BACK a reference!

Sorry. What I hadn't attached is the first part where the request is
built up:
    if Assigned(AData) then
    begin
      VHttp.RequestBody := TMemoryStream.Create;
      VJSON := AData.AsJSON;
      VHttp.RequestBody.Write(Pointer(VJSON)^, Length(VJSON));
      VHttp.RequestBody.Position := 0;
    end;

... changing the parameter to var...

function HttpRequestWithData(var AData: TJSONData; const AUrl: string;
  const AMethod: TRequestMethod; const AContentType: string): THttpResult;

... and changing the calling objects from TJSONObject to TJSONData
seemed to do the trick.

Thanks a lot.




More information about the fpc-pascal mailing list