#1 2016-01-28 08:22:09

swierzbicki
Member
Registered: 2014-11-19
Posts: 28

JSon class Serialization help

Hello,

I need some help with JSon class serialization.
I need to consume data from a Glassfish REST server.

I'm getting such JSon string from one REST GET resource :

{"admin":false,"fdId":37,"id":17,"name":"Jean-Pierre LeGros","societes":["MANGER_SA","BOIRE_SAS"],"user":"jplegros"}

This is how I've created the class :

TUser = class(TSQLRecord)
  private
    FAdmin: Boolean;
    FFdId: Extended;
    FId: Extended;
    FName: String;
    FSocietes: TArray<String>;
    FUser: String;

  public
    property admin: Boolean read FAdmin write FAdmin;
    property fdId: Extended read FFdId write FFdId;
    property id: Extended read FId write FId;
    property name: String read FName write FName;
    property societes: TArray<String> read FSocietes write FSocietes;
    property User: String read FUser write FUser;
  end;

This code return an empty class. ( TUser is has one empty record when modifying the JSon string : '[' + JSON + ']' )

var
  User: TUser;
  JSON : RawUTF8;
begin
  JSON := {"admin":false,"fdId":37,"id":17,"name":"Jean-Pierre LeGros","societes":["MANGER_SA","BOIRE_SAS"],"user":"jplegros"};
  User:= TUser.CreateAndFillPrepare(JSON);
  try
    while User.FillOne do
      begin
        Memo1.Lines.Add('Id : ' + User.id.ToString);
        Memo1.Lines.Add('User Name: ' + User.name);
      end;
  finally
    User.Free;
  end;
end;

What is wrong in my code ?

Last edited by swierzbicki (2016-01-28 08:24:00)

Offline

#2 2016-01-28 08:28:04

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

Re: JSon class Serialization help

Did you try to debug it in the IDE?
This is a good way of knowing more about the framework.

BTW, "extended" type is not handled. You should use either double, Integer, Int64 or even string.

Offline

#3 2016-01-28 10:43:15

swierzbicki
Member
Registered: 2014-11-19
Posts: 28

Re: JSon class Serialization help

So far :

- all String type with RawUTF8 type.
- all extended type replaced with double type.

I did some debugging and found that TSQLRecordFill.AddMap never map as it looks like the column name isn't a valid field !

Offline

#4 2016-01-28 12:23:48

swierzbicki
Member
Registered: 2014-11-19
Posts: 28

Re: JSon class Serialization help

I guess I'm close to get this working.
My problem was that fields where public instead of published and thus not visible...

  public
    property id: Double read FId write FId;
  published
    property admin: Boolean read FAdmin write FAdmin;
    property fdId: Double read FFdId write FFdId;
    property name: RawUTF8 read FName write FName;
    property societes: TRawUTF8DynArray read FSocietes write FSocietes;
    property User: RawUTF8 read FUser write FUser;

The only issue (and big one) remaining is the impossibility to use my id property (and not mormot internal ID)
I'm fetching my JSON string from an REST service developed with JAVA. I don't have my hands on it. How can I set "id" and how to get "id" in lowercase when getting TSQLRecord JSon string ?

Last edited by swierzbicki (2016-01-28 12:25:44)

Offline

#5 2016-01-28 13:19:28

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

Re: JSon class Serialization help

Did you try to map the fields (if they are external) using the MapField() method?
See http://synopse.info/files/html/Synopse% … l#TITL_120

Offline

Board footer

Powered by FluxBB