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 @   delphi中间件  阅读(135)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2022-02-22 brookframework
2022-02-22 ubuntu安装delphi需要的开发包
点击右上角即可分享
微信分享提示