Automatic JSON serialization of record or dynamic arrays via Enhanced RTTI
Since Delphi 2010, the compiler generates additional RTTI at compilation, so
record fields are described, and available at
By the way, this enhanced RTTI is one of the reasons why executables did grow so much in newer versions of the compiler.
SynCommons.pas unit is now able to use this enhanced
information, and let any
record be serialized via
RecordSave() functions, and all
internal JSON marshalling process.
In short, you have nothing to do.
Just use your
record as parameters, and, with Delphi 2010 and up,
they will be serialized as valid JSON objects.
Of course, text-based
definition or callback-based registration are still at hand, and will be
used with older versions of Delphi.
But you could be used to by-pass or extend the enhanced-RTTI serialization, even on newer versions of the compiler.
Enhanced RTTI support for records and dynamic arrays was added by this commit.
The documentation has been enhanced in synch!
Please ensure that you downloaded the latest SAD 1.18 pdf revision!
Serialization for older Delphi versions
Sadly, the information needed to serialize a
available only since Delphi 2010.
If your application is developped on any older revision (e.g. Delphi 7,
Delphi 2007 or Delphi 2009), you won't be able to automatically serialize
records as plain JSON objects directly.
You have several paths available:
- By default, the
recordwill be serialized as binary, and encoded as Base64 text;
- Or you can define method callbacks which will write or read the data as you expect;
- Or you can define the
recordlayout as plain text.
Note that any custom serialization (either via callbacks, or via text
definition), will override any previous registered method, even the mechanism
using the enhanced RTTI.
You can change the default serialization to easily meet your requirements.
For instance, this is what
SynCommons.pas does for any
TGUID content, which is serialized as the standard JSON text
"C9A646D3-9C61-4CB7-BFCD-EE2522C8F633"), and not
TGUID record layout as defined in the RTTI , i.e.
"D1":12345678,"D2":23023,"D3":9323,"D4":"0123456789ABCDEF" - which
is far from convenient.