










unit core.recordModel; // cxg 2026 {$ifdef fpc} {$mode delphi}{$H+} {$endif} interface uses Classes, SysUtils {$ifndef fpc} , json.Serializers {$else} , fpjson, fpjsonrtti, jsonparser, fpjson.helper {$endif} ; type ByteStr = RawByteString; PByteStr = PRawByteString; // record<-->json(binary) type TRecordSerial<T> = record public // json-->record class procedure unjson(const AJson: string; var AResult: T); static; // record-->json class procedure json(const ARecord: T; var AResult: string); static; //record-->binary class procedure bytes(const ARecord: T; var AResult: TBytes); static; class procedure bytestr(const ARecord: T; var AResult: ByteStr); static; class procedure stream(const ARecord: T; var AResult: TStream); static; //binary-->record class procedure unbytes(const ABytes: TBytes; var AResult: T); static; class procedure unbytestr(const AByteStr: ByteStr; var AResult: T); static; class procedure unstream(const AStream: TStream; var AResult: T); static; end; implementation { TRecordSerial<T> } class procedure TRecordSerial<T>.json(const ARecord: T; var AResult: string); {$ifdef fpc} var js: TJSONStreamer; {$endif} begin if @ARecord = nil then Exit; {$ifndef fpc} var LJsonSerializer: TJsonSerializer := TJsonSerializer.Create; try AResult := LJsonSerializer.Serialize<T>(ARecord); finally LJsonSerializer.Free; end; {$else} js := TJSONStreamer.Create(nil); try AResult := js.RecordToJSONString(@ARecord, typeinfo(T)); finally js.Free; end; {$endif} end; class procedure TRecordSerial<T>.stream(const ARecord: T; var AResult: TStream); begin if @ARecord = nil then Exit; if AResult = nil then AResult := TMemoryStream.Create; AResult.Write(ARecord, SizeOf(ARecord)); AResult.Position := 0; end; class procedure TRecordSerial<T>.bytes(const ARecord: T; var AResult: TBytes); var len: Integer; begin if @ARecord = nil then Exit; len := SizeOf(ARecord); SetLength(AResult, len); Move(ARecord, PByte(AResult)^, len); end; class procedure TRecordSerial<T>.bytestr(const ARecord: T; var AResult: RawByteString); var len: Integer; begin if @ARecord = nil then Exit; len := SizeOf(ARecord); SetLength(AResult, len); Move(ARecord, PByteStr(AResult)^, len); end; class procedure TRecordSerial<T>.UnJson(const AJson: string; var AResult: T); {$ifdef fpc} var js: TJSONDeStreamer; LRecord: T; {$endif} begin if AJson = '' then Exit; {$ifndef fpc} var LJsonSerializer: TJsonSerializer := TJsonSerializer.Create; try AResult := LJsonSerializer.Deserialize<T>(AJson); finally LJsonSerializer.Free; end; {$else} js := TJSONDeStreamer.Create(nil); try js.JSONToRecord(AJson, @AResult, typeinfo(AResult)); finally js.Free; end; {$endif} end; class procedure TRecordSerial<T>.unstream(const AStream: TStream; var AResult: T); begin if AStream = nil then Exit; AStream.Read(AResult, AStream.Size); end; class procedure TRecordSerial<T>.unbytes(const ABytes: TBytes; var AResult: T); var len: Integer; begin len := Length(ABytes); if len = 0 then Exit; Move(PByte(ABytes)^, AResult, len); end; class procedure TRecordSerial<T>.unbytestr(const AByteStr: RawByteString; var AResult: T); var len: Integer; begin len := Length(AByteStr); if len = 0 then Exit; Move(PByteStr(AByteStr)^, AResult, len); end; end.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。