#1 Yesterday 04:07:42

larand54
Member
Registered: 2018-12-25
Posts: 111

How to add a new json item into a jsonarray

Ive been working for hours to make it work search through google without finding anything that work. Google AI answers looks nice but they are compleatly wrong, all of them.
So I finally gave up and hope I can get an answer here.

Some code:

type
  TOrmFamily = class(TOrm)
    private
      fFamilyName: RawUTF8;
      fMembers: Variant;
    published
      property FamilyName: RawUTF8 read fFamilyName write fFamilyName;
      property Members: variant read fMembers write fMembers;
  end;
  TFamilyArray = array of TOrmFamily;

  TMember = Object//packed record
    Name: RawUTF8;
    Age: integer;
  end;
  
  fam1 := TOrmFamily.Create;
  fam1.FamilyName := 'Larson';
  fam1.fMembers := '';
  addFamily(fam1);

  mbr11.Name := 'Tommy'; mbr.Age := 45;
  addMember(fam1, mbr11);
  mbr12.Name := 'Linda'; mbr.Age := 43;
  addMember(fam1, mbr12);

function TForm2.addFamily(const pmcFam: TOrmFamily): boolean;
var
  ID: TID;
begin
  ID := Server.Server.orm.Add(pmcFam,true);
  result := ID > 0;
end;

function TForm2.addMember(const pmcFam: TOrmFamily;
  const pmcMbr: TMember): boolean;
var
  Jss, js: RawByteString;
  DocVariant: TDocVariant;
  v: variant;
  doc: TDocVariantData;
begin
end;

Expected result:
[{"name":"Tommy","age":45},{"name":"Linda","age":43}]]

It's no idea to show all tests i've done here but it is the function "addMember" where I like to make this work. It show some variables I used in my tests.


Delphi-11, WIN10

Offline

#2 Yesterday 06:43:50

larand54
Member
Registered: 2018-12-25
Posts: 111

Re: How to add a new json item into a jsonarray

I couldn't give up compleatly and I found an article which helped me finally.
link: https://blog.synopse.info/?post/2024/02 … hi-and-FPC

I'll put my solution here but want to ask for comment on this solution and if there are better way to do it.

function TForm2.addMember(const pmcFam: TOrmFamily;
  const pmcMbr: TMember): boolean;
var
  Jss, js, js2: RawByteString;
  val: variant;
  list,list2: IDocList;
  Dict: IDocDict;
  f: TDocDictFields;

  DocVariant: TDocVariant;
  v: variant;
  doc: TDocVariantData;
begin
  Js := RecordSaveJson(pmcMbr, TypeInfo(TMember));
  val := _JsonFast(js);
  js2 := _JsonFast(pmcFam.Members);
  list := DocList(js2);
  List.Append(val);
  v := List.AsVariant;
    if list.Len > 0 then
    while list.PopItem(v) do
    begin
      assert(list.Count(v) = 0); // count the number of appearances
      assert(not list.Exists(v));
      Listbox1.Items.Add(v.Name); // late binding
      dict := DocDictFrom(v); // transtyping from variant to IDocDict
      // enumerate the key:value elements of this dictionary
      for f in dict do
      begin
        Listbox2.Items.Add(f.Key);
        Listbox3.Items.Add(f.Value);
      end;
    end;
end;

Delphi-11, WIN10

Offline

#3 Yesterday 07:53:34

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,266
Website

Re: How to add a new json item into a jsonarray

I would add a TOrmFamily.AddMember() helper method.

Something like this (not tested):

procedure TOrmFamily.AddMember(member: TMember);
begin
  TDocVariantData(fMembers).AddItem(_ObjFast(RecordSaveJson(member, TypeInfo(TMember))));
end;

I may add a AddItemRtti() method for simplicity.

Offline

Board footer

Powered by FluxBB