mormot2.binary.serial.pas

mormot2.binary.serial.pas

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

interface

uses
  mormot.core.base, mormot.core.data, SysUtils;

type

  { TSerial }

  TSerial = class
    class function marshal<T>(const aRec: T): TBytes;
    class function marshal2<T>(const aRec: T): RawByteString;
    class function marshalbase64<T>(const aRec: T): RawUtf8;
    class function unmarshal<T>(const aBin: TBytes): T;
    class function unmarshal2<T>(const aBin: RawByteString): T;
    class function unmarshalbase64<T>(const aBin: PAnsiChar; const aLen: Integer): T;
  end;

implementation

{ TSerial }

class function TSerial.marshal2<T>(const aRec: T): RawByteString;
begin
  result := mormot.core.data.RecordSave(aRec, TypeInfo(T));
end;

class function TSerial.marshal<T>(const aRec: T): TBytes;
begin
  result := mormot.core.data.RecordSaveBytes(aRec, TypeInfo(T));
end;

class function TSerial.marshalbase64<T>(const aRec: T): RawUtf8;
begin
  result := mormot.core.data.RecordSaveBase64(aRec, TypeInfo(T));
end;

class function TSerial.unmarshal2<T>(const aBin: RawByteString): T;
begin
  mormot.core.data.RecordLoad(result, aBin, TypeInfo(T));
end;

class function TSerial.unmarshal<T>(const aBin: TBytes): T;
var
  raw: RawByteString;
  len: Integer;
begin
  len := Length(aBin);
  SetLength(raw, len);
  Move(aBin[0], PRawByteString(raw)^, len);
  mormot.core.data.RecordLoad(result, raw, TypeInfo(T));
end;

class function TSerial.unmarshalbase64<T>(const aBin: PAnsiChar; const aLen: Integer): T;
begin
  mormot.core.data.RecordLoadBase64(aBin, aLen, Result, TypeInfo(T));
end;

end.

 

posted @ 2023-02-22 09:04  delphi中间件  阅读(130)  评论(0编辑  收藏  举报