通过 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.

posted on 2009-10-27 15:21  万一  阅读(10322)  评论(3编辑  收藏  举报