#1 2018-07-03 16:53:54

macfly
Member
From: Brasil
Registered: 2016-08-20
Posts: 374

Return object properties as JSON ojbect

Hi Guys,

Considering these two related records:

TSQLUser...
property Name: RawUTF8;

TSQLProduct...
property Title : RawUTF8;
property User : TSQLUser;

ORM return the JSON for Joined methods in this way:

{
Product.ID: 1,
Product.Title: Test,
User.ID: 10,
User.Name : Jhon
}

Is there any way to return in this format?

{
   Product: {
      ID: 1,
      Title: Test...
   },
   User :{
      ID: 10,
      Name : Jhon
   }
}

Offline

#2 2018-07-03 17:56:15

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

Re: Return object properties as JSON ojbect

CreateJoined?

Offline

#3 2018-07-03 18:12:35

macfly
Member
From: Brasil
Registered: 2016-08-20
Posts: 374

Re: Return object properties as JSON ojbect

Yes, both CreateJoined and CreateAndFillPrepareJoined dont return related records as JSON objects.

AProduct := TSQLProduct.CreateJoined(Client,1);
SynSQLite3.TSQLDatabase(01ABA6D0) [{"Product.RowID":1,"Product.CreatedAt":135451071709,"Product.ModifiedAt":135451071709,"User.RowID":1,"User.CreatedAt":135451071709,"User.ModifiedAt":135451071709}] 

Last edited by macfly (2018-07-03 18:13:06)

Offline

#4 2018-07-03 18:31:39

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

Re: Return object properties as JSON ojbect

Of course!
What you see here is the result of the JOINed SQL query.
So field names have been "flattened", as expected.

If you want the nested JSON objects, then use ObjectToJSON(AProduct) after it has been properly filled...

And anyway, I would almost never use such joined for nested objects, but add some variant properties containing TDocVariant JSON content.
That is, aggregate documents may have a better use than joined tables, when you work with objects.

Offline

#5 2018-07-03 18:48:26

macfly
Member
From: Brasil
Registered: 2016-08-20
Posts: 374

Re: Return object properties as JSON ojbect

Thanks.

I need to return an array of json with nested objects, and using this approach works:

 AProdList := TList.Create;
    try
      AProduct := TSQLProduct.CreateAndFillPrepareJoined(Server,'WHERE ... LIMIT...',[],[]);
      try
        while AProduct.FillOne do
          AProdList.Add(AProduct);
        Result := ObjectToJSON(AProdList);
      finally
        AProduct.Free;
      end;
    finally
      AProdList.Free;
    end;  

It is not easy to break the paradigm and use only sharding. sad
But I'm trying smile

Last edited by macfly (2018-07-04 02:23:13)

Offline

#6 2018-07-03 23:08:13

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

Re: Return object properties as JSON ojbect

Your code will randomly trigger access violations, since you make AProduct.Free whereas AProduct is still in AProdList then used later on...

You should better use a TObjectList owning the items.

Offline

#7 2018-07-04 02:25:24

macfly
Member
From: Brasil
Registered: 2016-08-20
Posts: 374

Re: Return object properties as JSON ojbect

This is not the actual code, but thanks for the note.

Fix it up there

Offline

Board footer

Powered by FluxBB