mormot2 json操作
本文非完全原創,本文部分內容來自博客園,作者:{詠南中間件}
以下功能利用mORMot2實現 記錄
和 對象
的JSON序列化和反序列化。
unit mormot2.json.serial;
interface
uses
Classes, SysUtils,
mormot.core.buffers,
mormot.core.text,
mormot.core.json,
mormot.core.base
//
;
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.