#1 2022-06-11 18:20:58

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 430

Get a Ilist<> from json

I am trying to have a general procedure to fill an ilist<> from a json string
The following code seems to fill the ilist<> (it has correct amount of items) but when trying to access the items in the ilist<> I am getting a access violation error and contents are garbage.

procedure GetIList(Table: TOrmClass; var list; const json: Rawjson);
var plist: TIListParent; 
begin IInterface(list):=nil; 
plist:=Table.NewIList(list);
plist.Data.LoadFromJson(Putf8char(json));
end;

The json is produced by a RetrieveListJson from a other call
What am I missing?
Thank you in advance

Last edited by dcoun (2022-06-11 18:51:59)

Offline

#2 2022-06-12 06:44:15

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

Re: Get a Ilist<> from json

It is very likely that you are messing with interface references.
Using low-level methods like NewIList() may be tricky.

It is also unsafe that LoadFromJson() directly use PUtF8Char(Json) because, as the documentation states:

    // - warning: the content of P^ will be modified during parsing: please
    // make a local copy if it will be needed later (using e.g. TSynTempBufer)

So make a local copy of the JSON before. I will add an overloaded method with https://github.com/synopse/mORMot2/commit/de043b7d

Offline

#3 2022-06-12 09:04:36

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 430

Re: Get a Ilist<> from json

Thank you @ab.
I tried first with a local rawutf8 variable but the same happens. Also the same problem happens with the new commit
The problem should be in TRttiJson.ValueLoadJson or somewhere in json parsing.
As I said I am using the result of a Retrievelistjson from an other retrieval that is like the following:

[{"ID":-1,"bcode":269,"galid":0,"cnameonly":"ΓΑΛΗΝΙΚΟ","formcod":""},{"ID":2,"bcode":269,"galid":0,"cnameonly":"ΓΑΛΗΝΙΚΟ","formcod":""}]

Is something missing from TIListParent's processing?

Last edited by dcoun (2022-06-12 09:08:25)

Offline

#4 2022-06-12 09:33:42

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 430

Re: Get a Ilist<> from json

Trying an other Torm class I do not get an access violation, but only the ID is correct, all the other is garbage

Try this orm class

  TOrmApicities=class(Torm)
    private
      Fnam:rawutf8;
      Fnomosid:ptrint;
      Fact:boolean;
    published
      property nam:rawutf8 index 250 read Fnam write Fnam;
      property nomosid:ptrint read Fnomosid write Fnomosid;
      property act:boolean read Fact write Fact;
  end;

With the following json data:

[{"ID":1,"nam":"ΑΘΗΝΑ","nomosid":5,"act":1},{"ID":2,"nam":"ΠΕΡΙΣΤΕΡΙ","nomosid":5,"act":1},{"ID":3,"nam":"ΜΑΡΟΥΣΙ","nomosid":5,"act":1},{"ID":4,"nam":"ΑΙΓΑΛΕΩ","nomosid":5,"act":1},{"ID":5,"nam":"ΑΓΙΑ ΠΑΡΑΣΚΕΥΗ","nomosid":5,"act":1},{"ID":6,"nam":"ΧΑΛΑΝΔΡΙ","nomosid":5,"act":1}]

Last edited by dcoun (2022-06-12 09:36:19)

Offline

#5 2022-06-13 10:01:11

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 430

Re: Get a Ilist<> from json

The idea is to have a generic function to create and fill a Ilist<Torm>  from a json array, without using generics in the function definition
Reading from mormot2's code, I found that I can create a TIListParent with plist:=Table.NewIList(list);
Reading fast data with plist.Data.LoadFromJson(json); from json that came from a Retrievelistjson does not work. Only ID is set, all published properties from torm objects are not.
Does it need something special in the Json array?
Does plist.data or ilist<Trom> need something more before/after LoadFromJson?

Thank you in advance

Last edited by dcoun (2022-06-13 10:01:24)

Offline

#6 2022-06-13 11:26:54

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

Re: Get a Ilist<> from json

The problem is in TOrm.NewIList, which uses TypeInfo(TOrmObjArray) which is wrong when using low-level RTTI methods.
I am currently fixing it.

Edit: Should be fixed now with latest commits.
I have also included some associated regression tests.

Offline

#7 2022-06-13 15:03:15

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 430

Re: Get a Ilist<> from json

Now it works perfectly!!!
Thank you a lot @ab

Offline

Board footer

Powered by FluxBB