#1 Re: mORMot 2 » lists in objects to Json » 2025-10-02 07:59:35

Thanks a lot!

Somehow my brain doesn't like the square braces for this purpose. It seems to be linked to "access array / list element by position / memory offset" (I have to look at much C Code for embedded Systems). I think in this place the GetS() is exactly the same. Are there other places where it's worth to untie my brains knot ?

dict.PathDelim := '.';
Memo2.lines.add('short oneliner: ' + dict.GetS('measurementData.1.note'));      

Best regards

#2 Re: mORMot 2 » lists in objects to Json » 2025-10-01 14:01:17

I have not yet all my use cases covered but the question how to access "note" from the inner ojbect is solved:
I used IDocList / Dict and it works like a breeze big_smile

dict := DocDict('{"text":"Some general Annot. Description","measurementData":[{"id":47,"note":"erster Teil","values":[1.1,1.2,1.3]},{"id":11,"note":"zweiter Teil","values":[2.1,2.2,2.3]}]}');
Memo2.lines.add('oneliner note' + dict.GetL('measurementData').GetD(1).GetS('note')); 

#3 Re: mORMot 2 » lists in objects to Json » 2025-10-01 07:13:27

Thanks to both of you and especially flydev for this turnkey solution - superb!

Now I'm running into the next Issue I dont understand. Since JSON creation works so great I'm trying to read it back.
ObjLoadJson works fine if JSON and Object structure exactly match. However I need to tolerate if it is (slightly) modified by the user or if there is an update in the structure I need to read an old one or introduce new fields.

I wanted to do that with TDocVaraiant, but I cant Acces fields in the "measurementData" array of objects.

TDocVariantData(item).S['note'] fails in mormot.core.variants TDocVariantData.GetValueIndex because it expects cardinal(VType) = DocVariantVType, which is in my case (16396 = 271, obviously fail -> result-1)

procedure TForm1.Button9Click(Sender: TObject);
Var
  doc: variant;
  item: variant;
Begin
  doc := _Json('{"text":"Some general Description","measurementData":[{"id":47,"note":"erster Teil","values":[1.1,1.2,1.3]},{"id":11,"note":"zweiter Teil","values":[2.1,2.2,2.3]}]}');
  item := TDocVariantData(doc).A['measurementData'].Value[1];

  Memo2.Lines.Clear;
  Memo2.lines.add('Raw second element: ' + VariantToUtf8(TDocVariantData(doc).A['measurementData'].Value[1]));    //returns {"id":11,"note":"zweiter Teil","values":[2.1,2.2,2.3]}
  Memo2.lines.add('note: ' + TDocVariantData(item).S['note']);  //fails "property note not found"
End;  

#4 Re: mORMot 2 » lists in objects to Json » 2025-09-27 10:40:58

Thank you for you answer,

I'm lost in some rabbit hole wink Every hour I'm working with it I'm more confused.
I'm using Lazarus 4.2 FPC 3.2.2

At least for now I put everything into records, and used TArray instead of IList. My best guess so far is documented at https://github.com/D-H-R/mormot_json/bl … /unit1.pas.
I know that RecordSaveJson is supposed to do base64 encoding, I wasn't able to get s.th. else running sad

As you might have expected I need a human readable format.

Can you please point out to me which classes (for data holding) and functions for the "toJson" I should use for a first approach.

Thank you so much.

#5 mORMot 2 » lists in objects to Json » 2025-09-26 11:09:03

DirkH
Replies: 10

Hello, everyone

I want to store and retrieve the data of an object with a list property in a json string. However I have a hard time getting this to work. I would appreciate some hints were I can find examples or if it is a good practice using a list instead of an array at all.

Thanks and best regards

My current failing example code:

  TItem = Record
    Name: String;
  End;

  TStorage = Class
  Private
    fPlace: RawUtf8;
    fItems: IList < TItem > ;
  Published
    Property Place: RawUtf8 Read fPlace Write fPlace;
    Property Items: IList < TItem > Read fItems Write fItems;
  End; 
[...]
Procedure TForm1.Button1Click(Sender: TObject);
Var
  Storage: TStorage;
  Item: TItem;
Begin
  Storage := TStorage.Create;
  Storage.Place := 'Ocean';
  Storage.Items := Collections.NewPlainList < TItem > ;

  Item.Name := 'Foo';
  Storage.Items.Add(Item);
  Item.Name := 'Bar';
  Storage.Items.Add(Item);

  Memo1.text := ObjectToJson(Storage, [woHumanReadable]);      //does not print Items
End;      

Board footer

Powered by FluxBB