QMsgPack的用法DEMO
引用单元文件:
uses qstring, qmsgpack, qjson;
演示一:
procedure TForm2.Button10Click(Sender: TObject);
var
AJson, AItem: TQMsgPack;
S: String;
begin
AJson := TQMsgPack.Create;
try
AJson.Add('Item1', 0);
AJson.Add('Item2', true);
AJson.Add('Item3', 1.23);
for AItem in AJson do // 遍历
begin
S := S + AItem.Name + ' => ' + AItem.AsString + #13#10;
end;
mmResult.Lines.Add(S);
finally
AJson.Free;
end;
end;
演示二:
procedure TForm2.Button11Click(Sender: TObject);
var
AMsgPack: TQMsgPack;
ABytes: TBytes;
begin
AMsgPack := TQMsgPack.Create;
try
// 强制路径访问,如果路径不存在,则会创建路径,路径分隔符可以是./之一
AMsgPack.ForcePath('demo1.item[0].name').AsString := '1';
AMsgPack.ForcePath('demo1.item[1].name').AsString := '100';
try
ShowMessage('下面正常会抛出一个异常');
AMsgPack.ForcePath('demo1[0].item[1]').AsString := '200';
except
// 这个应该抛出异常,demo1是对象不是数组,所以是错的
end;
// 访问第6个元素,前5个元素会自动设置为null
AMsgPack.ForcePath('demo2[5]').AsInteger := 103;
// 强制创建一个空数组对象,然后调用Add方法添加子成员
AMsgPack.ForcePath('demo3[]').Add('Value', 1.23);
// 下面的代码将生成"demo4":[{"Name":"demo4"}]的结果
AMsgPack.ForcePath('demo4[].Name').AsString := 'demo4';
// 直接强制路径存在
AMsgPack.ForcePath('demo5[0]').AsString := 'demo5';
mmResult.Text := AMsgPack.AsString;
finally
AMsgPack.Free;
end;
end;
演示三:
procedure TForm2.Button12Click(Sender: TObject);
var
AMsgPack: TQMsgPack;
AList: TQMsgPackList;
begin
AMsgPack := TQMsgPack.Create;
try
with AMsgPack.ForcePath('object') do
begin
Add('name', 'object_1');
Add('subobj').Add('name', 'subobj_1');
Add('subarray', mptArray).AsVariant := VarArrayOf([1, 3, 4]);
end;
with AMsgPack.Add('array', mptArray) do
begin
Add.AsInteger := 100;
Add.AsInteger := 200;
Add.AsInteger := 300;
Add.Add('name', 'object');
end;
AList := TQMsgPackList.Create;
AMsgPack.ItemByRegex('na.+', AList, true);
mmResult.Lines.Add('ItemByRegex返回' + IntToStr(AList.Count) + '项结果');
AList.Free;
mmResult.Lines.Add('ItemByPath(''objectsubobj
ame'')=' +
AMsgPack.ItemByPath('objectsubobj
ame').AsString);
mmResult.Lines.Add('ItemByPath(''objectsubarray[1]'')=' +
AMsgPack.ItemByPath('objectsubarray[1]').AsString);
mmResult.Lines.Add('ItemByPath(''array[1]'')=' +
AMsgPack.ItemByPath('array[1]').AsString);
mmResult.Lines.Add('ItemByPath(''array[3].name'')=' +
AMsgPack.ItemByPath('array[3].name').AsString);
mmResult.Lines.Add('ItemByPath(''object[0]'')=' +
AMsgPack.ItemByPath('object[0]').AsString);
finally
AMsgPack.Free;
end;
end;
演示四:
procedure TForm2.Button13Click(Sender: TObject);
var
AMsgPack, AChild: TQMsgPack;
B: TBytes;
begin
AMsgPack := TQMsgPack.Create;
AMsgPack.ForcePath('a.b.c[].d').AsString := 'OK';
if AMsgPack.HasChild('a.b.c[0].d', AChild) then
mmResult.Text := AMsgPack.AsJson + #13#10 + '子结点 a.b.c[0].d 存在,值为:' +
AChild.AsString
else
mmResult.Text := AMsgPack.AsJson + #13#10 + '子结点 a.b.c[0].d 不存在';
AMsgPack.Free;
end;
演示五:
procedure TForm2.Button14Click(Sender: TObject);
var
APack, AItem: TQMsgPack;
begin
APack := TQMsgPack.Create;
try
with APack.Add('ID', mptArray) do
begin
Add.AsString := 'Name';
Add.AsString := 'Group';
Add.AsString := 'Readme';
end;
with APack.Items[0] do
begin
ShowMessage(Items[0].AsString);
end;
AItem := APack.Add;
AItem.KeyAsInteger := 30;
AItem.AsInteger := 100;
ShowMessage(APack.AsJson);
finally
FreeObject(APack);
end;
end;
演示六(APack.LoadFromFile):
procedure TForm2.Button1Click(Sender: TObject);
var
APack: TQMsgPack;
T, Speed: Cardinal;
AFileSize: Int64;
begin
APack := TQMsgPack.Create;
try
if dlgOpen.Execute then
begin
T := GetTickCount;
APack.LoadFromFile(dlgOpen.FileName);
T := GetTickCount - T;
AFileSize := GetFileSize(dlgOpen.FileName);
if T > 0 then
Speed := AFileSize * 1000 div T
else
Speed := 0;
mmResult.Lines.Add(dlgOpen.FileName + ' - 大小' + RollupSize(AFileSize) +
', 用时:' + IntToStr(T) + 'ms,速度:' + RollupSize(Speed));
end;
finally
APack.Free;
end;
end;
演示七(APack.Add('Stream').BytesFromStream):
procedure TForm2.Button2Click(Sender: TObject);
var
APack: TQMsgPack;
ABytes: TBytes;
S: String;
I: Integer;
AStream: TMemoryStream;
begin
APack := TQMsgPack.Create;
try
APack.Add('Age', 25);
APack.Add('Name', 'Jone smith');
with APack.Add('Colors', mptArray) do
begin
Add.AsString := 'Red';
Add.AsString := 'Yellow';
Add.AsString := 'Blue';
Add.AsBoolean := False;
end;
with APack.Add('Arm') do
begin
Add('Name').AsString := 'Hande';
Add('Fat').AsBoolean := False;
Add('Length').AsInteger := 32;
end;
SetLength(ABytes, 10);
for I := 0 to 9 do
ABytes[I] := I + 1;
APack.Add('Bytes', ABytes);
AStream := TMemoryStream.Create;
AStream.WriteBuffer(ABytes[0], 10);
APack.Add('Stream').BytesFromStream(AStream, 0); // 直接从文件加载可以用BytesFromFile
FreeObject(AStream);
mmResult.Lines.Clear;
S := APack.AsString;
mmResult.Lines.Add('Text,Size(' + IntToStr(Length(S)) + 'B)');
mmResult.Lines.Add(S);
ABytes := APack.Encode;
mmResult.Lines.Add('Binary,Size(' + IntToStr(Length(ABytes)) + 'B)');
mmResult.Lines.Add(qstring.BinToHex(@ABytes[0], Length(ABytes)));
mmResult.Lines.Add('[AsString]');
mmResult.Lines.Add(APack.AsString);
mmResult.Lines.Add('[AsJson]');
mmResult.Lines.Add(APack.AsJson);
finally
APack.Free;
end;
end;
演示八(AMsgPack.SaveToFile):
procedure TForm2.Button3Click(Sender: TObject);
var
AMsgPack: TQMsgPack;
I, J: Integer;
S: TBytes;
str: String;
const
BinarySizes: array [0 .. 8] of Integer = (2, 3, 4, 6, 8, 15, 16,
65535, 65536);
begin
AMsgPack := TQMsgPack.Create;
try
for I := 0 to 8 do
begin
SetLength(S, BinarySizes[I]);
for J := 0 to BinarySizes[I] - 1 do
S[J] := J mod 255;
AMsgPack.Add('bin_' + IntToStr(BinarySizes[I]), S);
end;
// 浮点
AMsgPack.Add('single', 1.23);
AMsgPack.Add('double', 100.001);
// 整数
AMsgPack.Add('int_8_127', 127);
AMsgPack.Add('int_8_n1', -1);
AMsgPack.Add('int_8_n32', -32);
AMsgPack.Add('int_8_n128', -128);
AMsgPack.Add('int_8_255', 255);
AMsgPack.Add('int_16_w2', 32767);
AMsgPack.Add('int_16_n2', -32768);
AMsgPack.Add('int_32_w4', 32768);
AMsgPack.Add('int_32_n4', -32769);
AMsgPack.Add('int_64_w2', Int64(MaxInt) + 1);
AMsgPack.Add('int_64_n2', Int64(-MaxInt) - 10);
// 布尔
AMsgPack.Add('bool_true', true);
AMsgPack.Add('bool_false', False);
// NIL
AMsgPack.Add('null');
// 字符串
AMsgPack.Add('str_short', '0123456789012345678901234567890'); // 31
SetLength(str, 255);
for I := 1 to 254 do
str[I] := Char(Ord('a') + (I mod 32));
AMsgPack.Add('str_b255', str);
SetLength(str, 65535);
for I := 1 to 65534 do
str[I] := Char(Ord('a') + (I mod 32));
AMsgPack.Add('str_b65535', str);
SetLength(str, 65537);
for I := 1 to 65536 do
str[I] := Char(Ord('a') + (I mod 32));
AMsgPack.Add('str_b65537', str);
// Small array
with AMsgPack.Add('array_I8', mptArray) do
begin
for I := 0 to 14 do
Add.AsInteger := I;
end;
with AMsgPack.Add('array_U8', mptArray) do
begin
for I := 0 to 16 do
Add.AsInteger := I;
end;
with AMsgPack.Add('array_I16', mptArray) do
begin
for I := 0 to 320 do
Add.AsInteger := I;
end;
with AMsgPack.Add('array_I32', mptArray) do
begin
for I := 0 to 65536 do
Add.AsInteger := I;
end;
with AMsgPack.Add('submap_I8') do
begin
for I := 0 to 14 do
Add('item_' + IntToStr(I), I);
end;
with AMsgPack.Add('submap_U8') do
begin
for I := 0 to 16 do
Add('item_' + IntToStr(I), I);
end;
with AMsgPack.Add('submap_I16') do
begin
for I := 0 to 320 do
Add('item_' + IntToStr(I), I);
end;
with AMsgPack.Add('submap_I32') do
begin
for I := 0 to 65536 do
Add('item_' + IntToStr(I), I);
end;
if not dlgSave.Execute then
exit;
AMsgPack.SaveToFile(dlgSave.FileName);
finally
AMsgPack.Free;
end;
end;
演示九(加载RECORD)
procedure TForm2.Button6Click(Sender: TObject);
var
ARec, ARec1: TRttiTestSubRecord;
AMsgPack, ACopy, AItem: TQMsgPack;
begin
{$IFNDEF UNICODE}
ShowMessage('本功能在当前IDE中不受支持.');
{$ELSE}
ARec.Int64Val := 1;
ARec.UInt64Val := 2;
ARec.UStr := 'Test String';
ARec.AStr := 'AnsiString';
ARec.SStr := 'ShortString';
ARec.IntVal := 3;
ARec.MethodVal := Button2Click;
ARec.SetVal := [{$IFDEF UNICODE}TBorderIcon.{$ENDIF}biSystemMenu];
ARec.WordVal := 4;
ARec.ByteVal := 5;
ARec.ObjVal := Button2;
ARec.DtValue := Now;
ARec.CardinalVal := 6;
ARec.ShortVal := 7;
ARec.CurrVal := 8.9;
ARec.EnumVal := {$IFDEF UNICODE}TAlign.{$ENDIF}alTop;
ARec.CharVal := 'A';
ARec.VarVal := VarArrayOf(['VariantArray', 1, 2.5, true, False]);
SetLength(ARec.ArrayVal, 3);
ARec.ArrayVal[0] := 100;
ARec.ArrayVal[1] := 101;
ARec.ArrayVal[2] := 102;
AMsgPack := TQMsgPack.Create;
try
{$IFDEF UNICODE}
AItem := AMsgPack.Add('Record');
AItem.FromRecord(ARec);
ACopy := AItem.Copy;
ACopy.ItemByName('Int64Val').AsInt64 := 100;
ACopy.ItemByPath('UStr').AsString := 'UnicodeString-ByJson';
ACopy.ItemByPath('AStr').AsString := 'AnsiString-ByJson';
ACopy.ItemByPath('SStr').AsString := 'ShortString-ByJson';
ACopy.ItemByPath('EnumVal').AsString := 'alBottom';
ACopy.ItemByPath('SetVal').AsString := '[biHelp]';
ACopy.ItemByPath('ArrayVal').AsVariant := VarArrayOf([10, 30, 15]);
// 或者
// with ACopy.ItemByPath('ArrayVal') do
// begin
// Clear;
// Add.AsInteger:=10;
// Add.AsInteger:=30;
// Add.AsInteger:=15;
// end;
// ACopy.ItemByPath('VarVal').AsVariant:=VarArrayOf(['By Json',3,4,false,true]);
// ACopy.ToRecord<TRttiTestSubRecord>(ARec1);
FreeObject(ACopy);
AMsgPack.Add('NewRecord').FromRecord(ARec1);
{$ENDIF}
mmResult.Lines.Add(AMsgPack.AsString);
finally
AMsgPack.Free;
end;
{$ENDIF}
end;