mormot2.json.serial

unit mormot2.json.serial;
/// <author>cxg 2023-2-11</author>
{$IFDEF fpc}
  {$MODE DELPHI}{$H+}
{$ENDIF}

interface

uses
  mormot.core.text, mormot.core.json, mormot.core.base,
  Classes, SysUtils;

type

  { TSerial }

  TSerial = class
    class function marshal<T>(const aRec: T): rawutf8;
    class function marshal2(const aObj: TObject): rawutf8;
    class function unmarshal<T>(const json: rawutf8): T;
    class procedure unmarshal2(const json: rawutf8; aObj: TObject);
  end;

implementation

{ TSerial }

class function TSerial.marshal2(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.unmarshal2(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.

 

posted @ 2023-02-20 12:23  delphi中间件  阅读(163)  评论(0编辑  收藏  举报