[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.
标签:core,const,rawutf8,mormot,json,mormot2,操作,class
From: https://www.cnblogs.com/hieroly/p/18155101