#1 2019-02-22 13:40:16

ertank
Member
Registered: 2016-03-16
Posts: 163

Help with json de-serialization

Hello,

I am using Lazarus 2.0.1, fpc 3.2.0, mORMot commit 1.18.5014. Platform is Raspberry Pi.

I have a pretty simple json, but I cannot handle it for a reason I don't know. I will try to keep my code examples minimum.

// json I am dealing is:
{
	"controller": true,
	"apiVersion": 4,
	"clock": 1550842219493,
	"databaseReady": true
}

type
  TController = packed record
    controller: Boolean;
    apiVersion: Integer;
    clock: UInt64;
    databaseReady: Boolean;
  end;

function IsControllerUp(): Boolean;
var
  Controller: TController;
  Json: string;
  J: RawUTF8;
begin
  Result := CallURL('controller', Json);  // do rest request and retrieve result string

  if Result then
  begin
    J := RawUTF8(Json);
    if RecordLoadJSON(Controller, J, TypeInfo(TController)) then
    begin
      Exit(Controller.controller);
    end
    else
    begin
      Exit(False);
    end;
  end;
end;

IsControllerUp() function above returns false and while debugging I see that RecordLoadJSON() return false and record is not modified at all.

I did try several things like using a RawUTF8 variable ("J" in above code for that matter) instead of typecasting, double checked my record definition. I could not see a problem.

Any help is appreciated.

Thanks & regards,
Ertan

Offline

#2 2019-02-22 16:13:13

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,238
Website

Re: Help with json de-serialization

There is no record RTTI in FPC.

You need to first register the record with TTextWriter.RegisterCustomJSONSerializerFromText.

Offline

#3 2019-02-22 16:52:54

ertank
Member
Registered: 2016-03-16
Posts: 163

Re: Help with json de-serialization

Thanks. That did solve my problem.

Is there a version of fpc which has record RTTI?

Offline

#4 2019-02-22 18:11:52

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,238
Website

Re: Help with json de-serialization

None yet, and it is not planned AFAIK.

Offline

Board footer

Powered by FluxBB