mormot2 model序列和还原
unit mormot2.json.serial;
/// <author>cxg 2023-6-4</author>
{$I def.inc}
interface
uses
mormot.core.buffers, mormot.core.text, mormot.core.json, mormot.core.base,
Classes, SysUtils;
type
{ TSerial }
TSerial = class
class function marshal<T>(const aRec: T): rawutf8; overload;
class function marshal(const aObj: TObject): rawutf8; overload;
class function unmarshal<T>(const json: rawutf8): T; overload;
class procedure unmarshal(const json: rawutf8; aObj: TObject); overload;
end;
implementation
{ TSerial }
class function TSerial.marshal(const aObj: TObject): rawutf8;
begin
Result := mormot.core.text.ObjectToJson(aObj);
end;
class function TSerial.marshal<T>(const aRec: T): rawutf8;
begin
result := mormot.core.json.RecordSaveJson(aRec, TypeInfo(T));
end;
class procedure TSerial.unmarshal(const json: rawutf8; aObj: TObject);
begin
mormot.core.json.ObjectLoadJson(aObj, json);
end;
class function TSerial.unmarshal<T>(const json: rawutf8): T;
begin
mormot.core.json.RecordLoadJson(result, json, TypeInfo(T));
end;
end.