delphi model序列和还原
封装了json和protobuf。
unit serialize; /// <author>cxg 2022-8-30</author> interface uses System.SysUtils, Grijjy.ProtocolBuffers, System.JSON.Serializers; type TSerial = class public //unmarshal class function unmarshal<T: record>(const value: string): T; overload; //json class function unmarshal<T: record>(const value: tbytes): T; overload; //protobuf //marshal class function marshal<T: record>(const aRecord: T): string; //json class function marshal2<T: record>(const aRecord: T): TBytes; //protobuf end; implementation class function TSerial.marshal<T>(const aRecord: T): string; begin var s: TJsonSerializer := TJsonSerializer.Create; try Result := s.Serialize<T>(aRecord); finally s.Free; end; end; class function TSerial.unmarshal<T>(const value: string): T; begin var s: TJsonSerializer := TJsonSerializer.Create; try Result := s.Deserialize<T>(value); finally s.free; end; end; class function TSerial.marshal2<T>(const aRecord: T): TBytes; begin var s: TgoProtocolBuffer := TgoProtocolBuffer.Create; try Result := s.Serialize<T>(aRecord); finally s.Free; end; end; class function TSerial.unmarshal<T>(const value: tbytes): T; begin var s: TgoProtocolBuffer := TgoProtocolBuffer.Create; try s.Deserialize<T>(result, value); finally s.Free; end; end; end.
标签:function,aRecord,const,delphi,value,序列,end,model,class From: https://www.cnblogs.com/hnxxcxg/p/17483322.html