我正在为Delphi学习一个很棒的框架mORMot,我找到了很多有用的函数来处理Variant
(在SynCommons中)。
现在我想更改Variant
的一些属性名称,但在搜索之后我还没有找到这样的函数,所以我决定写一个:
program Project7;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils, SynCommons;
function VariantChangeNames(var V: Variant; const FromNames, ToNames: TRawUTF8DynArray): Boolean;
var
Name: RawUTF8;
PData: PDocVariantData;
Val: Variant;
i, k: Integer;
begin
Result := False;
PData := DocVariantData(V);
i := 0;
for Name in FromNames do
begin
k := PData^.GetValueIndex(Name);
if k <> -1 then
begin
Val := PData^.Value[k];
PData^.Value[ToNames[i]] := Val;
PData^.Delete(k); // If delete before setting value, Val will be incorrect.
Result := True;
end;
Inc(i);
end;
end;
var
V: Variant;
begin
V := TDocVariant.New();
V.Name := 'John';
V.Age := 20;
VariantChangeNames(V, ['Name'], ['RealName']);
Writeln(VariantToString(V)); // {"Age":20,"RealName":"John"}
end.
VariantChangeNames
我写的可能效率不高,因为它需要同时执行delete
和add
。似乎直接修改VName
TDocVariantData
成员是最有效的方式,但它是私人成员。
有什么建议吗?提前谢谢。
BTW:mORMot官方论坛的邮件服务器似乎有问题,当我发布一个主题时,它显示:An error was encountered
Error: Could not connect to smtp host "217.70.184.11" (111) (Connection refused).