#1 2024-09-15 05:26:55

hieroly
Member
Registered: 2024-09-07
Posts: 3

Can mormot control default BASE64 encoding for record serialization?

hi AB!

type
  TTest = record
    id: Integer;
    name: string;
  end;

  TArrTest= array of TTest;
  TTests = record
    msg: string;
    t: TArrTest;
  end;  

   Found a strange problem using mormot.core.json RecordSaveJson(aRec, TypeInfo(T)), Convert the record to JSON serialization, using the code mormot.core.json.RecordLoadJson(result, json, TypeInfo(T));
It can also achieve deserialization, whether in Delhi or lazaarus, both directions are reversible!
But it is strange that the serialized text in Delphi is displayed normally, that is, it can be seen as a standard json format.
delphi  —— {"msg":"测试","t":[{"id":1,"name":"名字"}]}
lazarus —— "￰Bua1i+ivlQAOAQAAAAABAAAAAAAAAAblkI3lrZc="
However, when the serialized text is base64 under lazrus, it is not universally applicable. It is clearly inappropriate to directly use the system's DeBase64 for the serialized result.
Upon reviewing the code, I discovered a comment that states "will use default Base64 encoding". This is not what I had hoped for,
Is there a way to directly control the "default Base64 encoding" without writing custom code.

Additionally: mormot.core.json ObjectLoadJson(aObj, json) For Lazarus, it would be nice if $M+ could be used to define serialization for public member variables of classes. However, it was found that this only works for published properties of classes.

Last edited by hieroly (2024-09-15 05:29:33)

Offline

#2 2024-09-15 06:49:43

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

Re: Can mormot control default BASE64 encoding for record serialization?

This is a FPC limitation.
Since Delphi 2010, there is RTTI for records.
Stable FPC (3.2.x) does not have this RTTI. You need the trunk (3.2.3) version - we support it.

As the documentation, states, you need to:
- define the type as "packed"
- register your records using Rtti.RegisterFromText()

Note that Rtti.RegisterFromText() is also very useful on Delphi, when you want to customize the JSON output, e.g. the field names.
See e.g. how our OpenAPI generator defines its DTOs:
https://gist.github.com/synopse/b7e3251 … i-pas-L283

And we don't support RTTI for "public" fields on Delphi, because it would break a lot of existing code, and there is a "published" field for this purpose.
If you want to "publish" some existing field, inherit the class and move the property to the "publish" section.
Also note that extended RTTI for Delphi bloat the executable size. You can disable it for your mORMot projects.

Offline

#3 2024-09-21 22:48:26

RaelB
Member
Registered: 2010-08-04
Posts: 57

Re: Can mormot control default BASE64 encoding for record serialization?

ab wrote:

Stable FPC (3.2.x) does not have this RTTI. You need the trunk (3.2.3) version - we support it.

Hmmm, is trunk not 3.3.1?

Using fpcupdeluxe on Windows, I installed fpc trunk and lazarus trunk.
About says fpc 3.3.1 and Lazarus 3.99

I also tried to install fpc fixes-3.2 and lazarus stable.
In that case it says fpc is version 3.2.3, and lazarus is 3.4

In both cases, serialization to json outputs base64 string, not "normal" json.
If I use Rtti.RegisterFromText, then in all cases (i.e. also 3.2.2) it outputs normal json.

Sample code: serialize record with RecordSaveJson/DynArraySaveJson

Offline

#4 2024-09-26 06:53:50

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

Re: Can mormot control default BASE64 encoding for record serialization?

Yes, my bad, FPC trunk is 3.3.1.

IIRC I did implement and validate/test it for FPC trunk.
But you need to specifically tell the compiler to generate the extended RTTI for the record.
See https://wiki.lazarus.freepascal.org/FPC … ended_RTTI

Offline

#5 2024-09-26 12:03:35

RaelB
Member
Registered: 2010-08-04
Posts: 57

Re: Can mormot control default BASE64 encoding for record serialization?

Ok, I can confirm it works when including the directive: {$RTTI EXPLICIT FIELDS([vcPublic])}
as indicated in test.core.base.pas.
Pretty cool

Offline

Board footer

Powered by FluxBB