#1 Re: mORMot 2 » TOrm with T(Integer)DynArray JSON serialization issues » 2026-02-03 07:29:43

Ok variant works, but ... its a variant wink
i.e. string:=variant works at compile time, but obviously crashes at runtime. Since I like strong typing, I'd prefer to get a compiler error.

Question: Can I have something that is like a dynarray but serializes as human readable correct json instead of blob?
(Note: TStrings gets JSON : "StrArr": "[\"1\",\"2\"]", but I need "StrArr": "["1","2"]")

#2 Re: mORMot 2 » TOrm with T(Integer)DynArray JSON serialization issues » 2026-01-30 19:11:54

Just Found the New Documentation. Great Work!
According to https://github.com/synopse/mORMot2/blob … pter-05.md I think it is intended behaviour because dynarrays are stored as blob.

I'll try (TDoc)Variant. My arrays are typically length<10 and I need to exchange them with different external services via JSON so I think it might be a good solution.

#3 mORMot 2 » TOrm with T(Integer)DynArray JSON serialization issues » 2026-01-30 15:31:40

DirkH
Replies: 2

I want to have the TIntegerDynArray to be recognized as such, i.e. as JSON Array with ObjectToJson and in SQLite DB (for easier debugging).

What do I have to do? I think it might be related to RTTI in Lazarus 3.8 / FPC 3.2.2. If anyhow possible I'd like to stay with the current stable FPC release.

  TParameter = class(TOrm)
  private
    fParameterID: integer;
    fName: RawUtf8;
    fValue: RawUtf8;
    fIntArr: TIntegerDynArray;
  published
    property ParameterID: integer read fParameterID write fParameterID stored AS_UNIQUE;
    property Name: RawUtf8 read fName write fName;
    property Value: RawUtf8 read fValue write fValue;
    property IntArr: TIntegerDynArray read fIntArr write fIntArr;
  end;  

Showmessage(Param.DynArray('IntArr').SaveToJson()); // returns [10,20,30,99]. 
ObjectToJson(TParameter); // is .. "IntArr": "￰AAADAAAAAAoAAAAUAAAAHgAAAA==" (same for entry in sqlite DB)

#4 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

#5 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')); 

#6 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;  

#7 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.

#8 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