gpt4 book ai didi

Delphi XE2 Datasnap TObject 与 TMemoryStream

转载 作者:行者123 更新时间:2023-12-02 01:26:29 26 4
gpt4 key购买 nike

如何通过datasnap与TMemoryStream传输TObject?

TMyObject = class(TBizObject)
FText: TMemoryStream; <-- Content not sent
...
end

在服务器端

TServerMethods.GetMyClass(const AId: Integer): TMyObject
begin
Result := TMyObject.find(AId);
end;

在客户端 TMyObject 已正确解码,但 FText 内容为空。

TMemoryStream Json内容

"FText": {
"type":"System.Classes.TMemoryStream",
"id":2,
"fields":{
"FCapacity":16384,
"FSize":11788,
"FPosition":11788
}
}

最佳答案

关键是创建字段类型为*的TConverterEvent和TRverterEvent。如果没有源代码,很难找到解决方案。

procedure RegisterDatasnapConvertersAndReverters;
var
LConverter: TConverterEvent;
LReverter: TReverterEvent;
begin
LConverter := TConverterEvent.Create(TMemoryStream, '*' {FIELD_ANY});
LConverter.TypeStringConverter := function (Data: TObject): string
var
stream: TMemoryStream;
begin
stream := TMemoryStream(Data);
stream.Position := 0;
Result := EncodeBase64(stream.Memory, stream.Size);
end;
TJSONConverters.AddConverter(LConverter);

LReverter := TReverterEvent.Create(TMemoryStream, '*' {FIELD_ANY});
LReverter.TypeStringReverter := function(Data: string): TObject
var
bytes: TBytes;
begin
bytes := DecodeBase64(Data);
Result := TMemoryStream.Create;
if nil <> bytes then
TMemoryStream(Result).WriteBuffer(bytes[0], Length(bytes));
end;
TJSONConverters.AddReverter(LReverter);
end;

initialization
RegisterDatasnapConvertersAndReverters;

关于Delphi XE2 Datasnap TObject 与 TMemoryStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10225425/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com