You are not logged in.
Pages: 1
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
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
Online
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
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
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
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.
Online
Now it works perfectly!!!
Thank you a lot @ab
Offline
Pages: 1