Data.FireDACJSONReflect Multi-dataset and compressed byte streaming encrypted JSon serialization-Json serialization of Delphi 10 big data implementation methods
uses Data.FireDACJSONReflect
// C:\Program Files (x86)\Embarcadero\Studio\20.0\source\data\datasnap\Data.FireDACJSONReflect.pas
TFDJSONInterceptor = class(TJSONInterceptor)
public
// Conversion utility methods
class function ItemListToJSONObject(const AList: TItemList; const AJSONObject: TJSONObject): Boolean; static;
class function DataSetsToJSONObject(const ADataSets: TFDJSONDataSetsBase; const AJSONObject: TJSONObject): Boolean; static;
class function JSONObjectToItemList(const AJSONObject: TJSONObject; const AList: TItemList): Boolean; static;
class function JSONObjectToDataSets(const AJSONObject: TJSONObject; const ADataSets: TFDJSONDataSetsBase): Boolean; static;
destructor Destroy; override;
// Reverter/Converter overrides
function TypeObjectConverter(AData: TObject): TObject; override;
function TypeObjectReverter(AData: TObject): TObject; override;
end;
Among them: ItemListToJSONObject
LJSONValue_ofDataSet := DataSetToJSONValue(LDataSet); //: TJSONValue;
//: function DataSetToJSONValue(const ADataSet: TFDAdaptedDataSet): TJSONValue;
->
S := DataSetToString(ADataSet);
//: function DataSetToString(const ADataSet: TFDAdaptedDataSet): string;
->
function DataSetToString(const ADataSet: TFDAdaptedDataSet): string;
var
LBinary64: string;
LMemoryStream: TMemoryStream;
LStringStream: TStringStream;
LDstStream: TMemoryStream;
Zipper: TZCompressionStream;
begin
LDstStream := TMemoryStream.Create;
try
LMemoryStream := TMemoryStream.Create;
try
ADataSet.SaveToStream(LMemoryStream, TFDStorageFormat.sfBinary);
LMemoryStream.Seek(0, TSeekOrigin.soBeginning);
Zipper := TZCompressionStream.Create(clDefault, LDstStream);
try
Zipper.CopyFrom(LMemoryStream, LMemoryStream.Size);
finally
Zipper.Free;
end;
finally
LMemoryStream.Free;
end;
LDstStream.Seek(0, TSeekOrigin.soBeginning);
LStringStream := TStringStream.Create;
try
TNetEncoding.Base64.Encode(LDstStream, LStringStream);
LBinary64 := LStringStream.DataString;
finally
LStringStream.Free;
end;
finally
LDstStream.Free;
end;
Result := LBinary64;
end;
There are several ways of Base64 encoding Encode and Decode encoding. Base64 is a method that can combine any Binary data with 64 characters into a string, and this Binary data and string data can be converted to each other, which is very convenient . In practical applications, in addition to visualizing Binary data , Base64 is also commonly used to represent the encrypted content of the string. Encoding with Base64 is unreadable and requires Decode to decode before reading. https://baike.baidu.com/item/base64/8545775?fr=aladdin
//: Return to the client: Binary Json like this is the streaming data of Bson data:
{
"result": [
{
"CtL00001": "eJztvQuYHMdxGDyASIikKJKiXrRESifZlkEbim/37gBcHMc+3AHEQfci7vgAE2cyNzu7O8TuzmJm\r\n9g5Hx4/Ejh3n5diOHb/imJEoy9GDT/AFkBAtkhIBUqDFl2RZ+ajo8dlxojhO+MuhZfLv7umZ6e6p\r\n6pnduwFACuBH4G6qu6enuqq6qrq6amJq1+KlxugVp67fbLxqbDI2vbr51TecZ1xmGMakERozxjD/\r\nr2KcDz7dQjoZxhsN+ucCw7jQ+Jnf+E3jIuPH32sYr77p1YvPM84jANeoke7xT1GXS4w3G5eSlgYZ\r\ndtNbjE2XG5veamx6m7Hp7car5M872N/vNDZdYWz6HmPTu4xN7zY2XZmMcRXr+Qp9wTvID7ZhknlZ\r\nxrLRMhyjQ35qk3/P18C2GJvZJN5jvNfYmUyCvJ68coi98kpN76tYn/exv9kkrsg0XSLzDNlP52uh\r\nW4w3JBOZQiai638V6/U+9jebysVJ4zWjy16vPtnC0Bi9cjPySrXPVazl+9jf7DUX8QY9I2AvkX/f\r\nQp7kvULukXkBxf5+ArANz/DJkk+Sf3tkBUK+rjBsC/kvS1zZdYV7C2RFf7iR4DlGovz7Fkby0fdh\r\nr5F7REO/L33BB8gPM2RBA/JeupyUrkzjOtK4Rp6G5Lcp9q9FXl687RbKhWRi7ze+lxEWNLHio11l\r\nvJO0/T42Epv0D+Z2XSR/+wSdNluo/tpvIfIjb/L9jZj5gCGw+wLp4JEB4mkXabWFLaV+skXGyUyR\r\nLs+UMUs62qShw79qDyPWReMQ4XqXPRnhhFGs7RbjTQnF/o9LcMIoNtpVbIz3sb9feeXV73/l1Q+8\r\nazI6arD5vIu08AlWGmwNIszGFPAeiQKyWLyC4X6Cjeqy2ftslAbBSbyGl/P5iLQq01vc8j1stCne\r\nZonRjMNGpbPp0qs1pL3FVuodygjx8ytBOowp5zLjevKTQ3AaP7mEYNQTft/Cf7+I/0sp/TIJQ/Q9\r\nV7B1aLGZRpiI+1+p8OQ+Psuo3yUS7gPj/wcCWOAL"
}
]
}
// : \r\n
\n is new line, English is New line. \r is carriage return, English is Carriage return.
It is recommended that everyone develop a habit, adjust the old code as soon as possible, and directly use mature middleware as soon as possible, such as Gao Yong's "three-tier middleware source code library", which is systematic, stable, safe, and has good concurrent performance.