[fpc-pascal] JSON me - blind again?

2013-03-12 Thread Reinier Olislagers
Hi list,

(FPC 2.6.0 or trunk)

Been fiddling a bit too long and strongly suspect PEBKAC/PICNIC (and my
seemingly perennial refusal to adapt to Object Pascal's way of dealing
with object references).

Thanks for your suggestions!


See the === marks below

var
  CurrentPage: integer;
  RequestResult: THttpResult;
  CommJSON: TJSONObject;
begin
  result:=INVALIDID;
  CommJSON:=TJSONObject.Create;
  try
try
// do an HTTP request and get a JSONData back.
//
RequestResult:=HttpRequestWithData(CommJSON,FCGIURL+'document/',rmPost);
  if RequestResult.Code200 then
  begin
//snip error handling
  end
  else
  begin
//=== the line below works - so CommJSON is a valid object, right?
if Assigned(CommJSON) then
begin
// however, the next line gives a runerror(210): no vmt found
  if (CommJSON.JSONType=jtObject) then
  begin
if (CommJSON.IndexOfName('documentid',false)-1) then
  result:=CommJSON.Integers['documentid'];
  end;
end;
  end;


I'm not confident about
function HttpRequestWithData(AData: TJSONData; const AUrl: string;
  const AMethod: TRequestMethod; const AContentType: string): THttpResult;
...
  FreeAndNil(AData);
  VData.Position := 0;
  VParser := TJSONParser.Create(VData);
  try
try
  AData := VParser.Parse;
// so it destroys the object passed to it and recreates it
// but AData.JSONType does seem to be JSONObject...
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


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

2013-03-12 Thread Michael Van Canneyt



On Tue, 12 Mar 2013, Reinier Olislagers wrote:


Hi list,

(FPC 2.6.0 or trunk)

Been fiddling a bit too long and strongly suspect PEBKAC/PICNIC (and my
seemingly perennial refusal to adapt to Object Pascal's way of dealing
with object references).

Thanks for your suggestions!


See the === marks below

var
 CurrentPage: integer;
 RequestResult: THttpResult;
 CommJSON: TJSONObject;
begin
 result:=INVALIDID;
 CommJSON:=TJSONObject.Create;
 try
   try
// do an HTTP request and get a JSONData back.
//
RequestResult:=HttpRequestWithData(CommJSON,FCGIURL+'document/',rmPost);
 if RequestResult.Code200 then
 begin
//snip error handling
 end
 else
 begin
//=== 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'.


   begin
// however, the next line gives a runerror(210): no vmt found
 if (CommJSON.JSONType=jtObject) then
 begin
   if (CommJSON.IndexOfName('documentid',false)-1) then
 result:=CommJSON.Integers['documentid'];
 end;
   end;
 end;


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!

Michael.

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal