#1 2020-01-21 14:43:51

mdbs99
Member
From: Rio de Janeiro, Brazil
Registered: 2018-01-20
Posts: 132
Website

JSON record serialization: I got an issue reading boolean values

Hello,
I'm trying to serialize a JSON into record with nested one, but RecordLoadJSON returns FALSE.

JSON (body.json file) is like this:

{
  "integrationId":19,
  "token":{
    "accessToken":"eyJhbGciOiJSUzI1NiIsInR5c",
    "createdIn":"2020-01-21 09:28:44",
    "expiresIn":"2024-12-25 09:28:44"
  },
  "success":true,
  "message":null
}

Then, I have TTokenResponse and TLoginResponse:

type
  TTokenResponse = packed record
    accessToken: RawUTF8;
    createdIn: TDateTime;
    expiresIn: TDateTime;
  end;

  TLoginResponse = packed record
    integrationId: Integer;
    token: TTokenResponse;
    sucess: boolean;
    message: RawUTF8;
  end;

initialization
  TTextWriter.RegisterCustomJSONSerializerFromText([
    TypeInfo(TTokenResponse), 'accessToken:RawUTF8 createdIn,expiresIn:TDateTime',
    TypeInfo(TLoginResponse), 'integrationId:Integer token:TTokenResponse sucess:boolean message:RawUTF8'
  ]);

If I remove "success" field from both (JSON and record), it works.

I'm using last version of mORMot but compiling on Delphi 7.

For testing, could be like this:

function Login: TLoginResponse;
var
  l: TRawUTF8List;
begin
  RecordZero(result, TypeInfo(TLoginResponse));
  l := TRawUTF8List.Create;
  try
    l.LoadFromFile('body.json');
    if not RecordLoadJSON(result, l.Text, TypeInfo(TLoginResponse)) then
      raise Exception.Create('Invalid...');
  finally
    l.Free;
  end;
end;

best regards.

Last edited by mdbs99 (2020-01-21 14:51:04)

Offline

#2 2020-01-21 15:05:17

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

Re: JSON record serialization: I got an issue reading boolean values

You have a typo in your code.

In the text description (and record type definition), it is sucess:boolean (single C) whereas in the JSON it is "success" (two C).

Offline

#3 2020-01-21 15:09:32

mdbs99
Member
From: Rio de Janeiro, Brazil
Registered: 2018-01-20
Posts: 132
Website

Re: JSON record serialization: I got an issue reading boolean values

Ops! the JSON was wrong, they changed but I forgot to change on the code... f*ck!

Thank you!!

Offline

Board footer

Powered by FluxBB