#1 Today 09:23:21

idigger
Member
Registered: 2022-11-05
Posts: 29

DynArrayLoadJson does not work in Delphi 2007

  TOrder = record
    id: Integer;
    amount: Integer;
  end;
  TOrders = array of TOrder; 

  s := '{"code":200,"orders":[{"id":1,"amount":1},{"id":2,"amount":2}]}';
  V := _Json(s);
  DynArrayLoadJson(orders, VariantSaveJson(V.orders), TypeInfo(TOrders));
  WriteLn(Length(orders));

The code above results in Length(orders)==0 in Delphi 2007, but 2 in Delphi 12 and Lazarus.

I remember it working fine before, but now it suddenly stops working. Even reverting to an older version of the mormot2 repository doesn't work anymore. It's really strange.

Additionally, in Lazarus, VariantSaveJson must be added before V.orders in the DynArrayLoadJson statement above, but not in Delphi.

Last edited by idigger (Today 09:27:04)

Offline

#2 Today 13:46:26

idigger
Member
Registered: 2022-11-05
Posts: 29

Re: DynArrayLoadJson does not work in Delphi 2007

The problem has been identified. In the above code, simply change "s" to "s := '{"code":200,"orders":["￰AAACAAAAAAEAAAABAAAAAQAAAAIAAAA="]}';" and there will be no issues. That Base64 string is the binary Base64 encoding of the "orders" array.


It would be much more convenient if the functions DynArraySaveJsonBase64 and DynArrayLoadJsonBase64 could be provided, just like the existing RecordSaveBase64/RecordLoadBase64 functions.

Last edited by idigger (Today 14:23:47)

Offline

#3 Today 16:48:46

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

Re: DynArrayLoadJson does not work in Delphi 2007

On Lazarus/FPC, register the record as text, and you will have proper JSON serialization.
Also note that the record should be marked as "packed".

Or switch to FPC trunk: it has RTTI for records.

Offline

#4 Today 17:09:47

idigger
Member
Registered: 2022-11-05
Posts: 29

Re: DynArrayLoadJson does not work in Delphi 2007

My backend service program is using the latest FPC trunk, but my front end is an old project and can only use Delphi 2007. Therefore, we can only hope to use DynArraySaveJsonBase64 in the backend and DynArrayLoadJsonBase64 in the front end.

Offline

Board footer

Powered by FluxBB