#1 Re: mORMot 1 » Automatic JSON serialization of record via Enhanced RTTI » 2021-06-16 12:13:12

Hello,

first of all I'd like to say that the mORMot is a great project and very useful.

Now my subject:

I was serializing a record (TTestJSON) with some "sub" records (TTestJSON2 and TTestJSON3) with RecordSaveJSON and the Option soWriteHumanReadable with RegisterCustomJSONSerializerSetOptions.
My problem was that the CustomParser with the option was only used for the record registered with RegisterCustomJSONSerializerSetOptions.
For Records, which are part of my so sad "base" record, the Option was not active.

That is due to the fact that for every found tkCustom type (record), a CustomParser is created and stored in TJSONCustomParsers.fParser. And those do not get the Options.
So if you have records inside records the options only apply to the record registered and not to the records inside the record.


This is my record:

type TaiFeedback_SAP_HU_Gen_Box = ARRAY[1..10] OF integer;
type TaxMAC_ProtectionDoorsClosed = ARRAY[0..15] OF Boolean;
type TrModule_CycleTime = ARRAY[0..10] OF REAL;
type TadwMAC_PartsInBoxCounter = ARRAY[1..20] OF DWORD;

TYPE TTestJSON3 = packed record

	bWriteSkValueToPackage			: BYTE;
  	aiFeedback_SAP_HU_Gen_Box		: TaiFeedback_SAP_HU_Gen_Box;
	strMsgText						: String; 	

end;
TYPE TTestJSON2 = packed record

	bWriteSkValueToPackage			: BYTE;
  	aiFeedback_SAP_HU_Gen_Box		: TaiFeedback_SAP_HU_Gen_Box;
  	strMsgText						: String; 	
  	sTestJSON3             : TTestJSON3;

end;


TYPE TTestJSON = packed record
	diMsgNr							: DINT;
	strMsgText						: String; 	
   	axMAC_ProtectionDoorsClosed     : TaxMAC_ProtectionDoorsClosed;	
	rModule_CycleTime				: TrModule_CycleTime;
	bWriteSkValueToPackage			: BYTE;
  	aiFeedback_SAP_HU_Gen_Box		: TaiFeedback_SAP_HU_Gen_Box;
	adwMAC_PartsInBoxCounter			: TadwMAC_PartsInBoxCounter;
	sTestJson2 : TTestJson2;
	bCrc							: BYTE;

end;

This is the implementation:

function writeJSONFromRecord(var Data; DataType : Pointer; filename : String; xHumanReadable : Boolean) : BOOLEAN;
var
   JsonStringRawUTF8 : RawUTF8;
   Options : TJSONCustomParserSerializationOptions;

begin
   result := false;

   if xHumanReadable then begin
      Options := [soWriteHumanReadable];
      if not TTextWriterWithEcho.RegisterCustomJSONSerializerSetOptions(DataType, Options, true) then
      exit;
   end;
   JsonStringRawUTF8 := RecordSaveJSON(Data, DataType);
 
end;

After that I changed some parts of the code of SynCommons and after that with an additional Boolean in RegisterCustomJSONSerializerSetOptions, it now registers all records inside the record with the Options.
My solution is probably not very good but at least it works. If somebody of the mORMot team wants the code, I would provide it smile
Correct me if I'm wrong but for me that was the only way.

Maybe a better useful solution would be an global Option variable for all Registered types without an manual set option? smile

Board footer

Powered by FluxBB