midas支持使用OLEVARIANT序列化对象,最新的DATASNAP支持使用OLEVARAINT和JSON来序列化对象。
下面的代码演示OLEVARINAT序列化TPARAMS, TPARAMETERS对象。OLEVARIANT本质上是一个BYTE数组。
还原TPARAMS对象
procedure VariantToParams(input:OleVariant;par:TParams);
// TParam 's property: fieldType, paramName, ParamType, value, size
// paramType default value ptinput
// size = sizeof(value)
var
n, i:integer;
begin
try
n:=0;
i:=0;
par.Clear;
while VarArrayHighBound(input,1)>=(n+3)do
begin
par.CreateParam(TFieldType(input[n+1]),input[n+2],ptInput);
par.Items[i].Value := input[n+3];
par.Items[i].Size :=SizeOf(input[n+3]);
n:=n+3;
i:=i+1;
end;
except
Exit;
end;
end;
序列化TPARAMS对象
function ParamsToVariant(par:TParams): OleVariant;
// TParam 's property: fieldType, paramName, ParamType, value, size
// paramType default value ptinput
// size = sizeof(value)
var
tmpv:OleVariant;
n,i:integer;
begin
try
tmpv:=VarArrayCreate([1,par.Count*3],VarVariant);
n:=0;
i:=0;
while par.Count>i do
begin
tmpv[n+1]:=Ord(par.Items[i].DataType);
tmpv[n+2]:=par.Items[i].Name;
tmpv[n+3]:=par.Items[i].Value;
i:=i+1;
n:=n+3;
end;
result:=tmpv;
except
Exit;
end;
end;
还原TPARAMETERS
procedure VariantToParameters(input:OleVariant;par:TParameters);
// TParameters's property: name, dataType, Direction, size, value
// direction default pdinput
// size = sizeof(value)
var
n:integer;
begin
try
n:=0;
par.Clear;
while VarArrayHighBound(input,1)>=(n+3)do
begin
par.CreateParameter(input[n+1],tfieldtype(input[n+2]),pdInput,SizeOf(input[n+3]),input[n+3]);
n:=n+3;
end;
Except
Exit;
end;
end;
序列TPARAMETERS对象
function ParametersToVariant(par:TParameters): OleVariant;
// TParameters's property: name, dataType, Direction, size, value
// direction default pdinput
// size = sizeof(value)
var
tmpv:OleVariant;
n,i:integer;
begin
try
tmpv:=VarArrayCreate([1,par.Count*3],VarVariant);
n:=0;
i:=0;
while par.Count>i do
begin
tmpv[n+1]:=par.Items[i].Name;
tmpv[n+2]:=Ord(par.Items[i].DataType);
tmpv[n+3]:=par.Items[i].Value;
i:=i+1;
n:=n+3;
end;
result:=tmpv;
except
exit;
end;