通過 SuperObject 的公用函數 SO 實現一個 ISuperObject 接口非常方便;
前面都是給它一個字符串參數, 它的參數可以是任一類型甚至是常數數組.
SA 和 SO 都是返回一 ISuperObject;
SO 可以產生一個 stArray 類型的 ISuperObject, 但 SA 只會產生 stArray 類型的 ISuperObject;
SA 的參數也只能是常數數組.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses SuperObject, TypInfo;
procedure TForm1.Button1Click(Sender: TObject);
var
jo: ISuperObject;
begin
jo := SO(['A',1, 'B',2.5, 'C','xyz', 'D',True]);
ShowMessage(jo.AsJSon); // {"D":true,"C":"xyz","B":2.5,"A":1}
{顯示類型名}
ShowMessage(GetEnumName(TypeInfo(TSuperType), Ord(jo.DataType))); // stObject
jo := SO(3.14);
ShowMessage(jo.AsJSon); // 3.14
{顯示類型名}
ShowMessage(GetEnumName(TypeInfo(TSuperType), Ord(jo.DataType))); // stDouble
jo := SA(['A',1, 'B',2.5, 'C','xyz', 'D',True]);
ShowMessage(jo.AsJSon);
{顯示類型名}
ShowMessage(GetEnumName(TypeInfo(TSuperType), Ord(jo.DataType))); // stArray
end;
//SA 再舉例
procedure TForm1.Button2Click(Sender: TObject);
var
jo,ja: ISuperObject;
begin
ja := SA(['x','y','z']);
jo := SO('{A:1, B:2}');
jo['B'] := ja;
ShowMessage(jo.AsJSon); // {"B":["x","y","z"],"A":1}
end;
end.