x-superobject

GITHUB:

https://github.com/onryldz/x-superobject

**Delphi Cross Platform Rapid JSON**
------------------------------------
[![](https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=7BPTUUP4DGD5C)

###Basic
```json
{
"name": "Onur YILDIZ",
"vip": true,
"telephones": ["000000000", "111111111111"],
"age": 24,
"size": 1.72,
"adresses": [
{
"adress": "blabla",
"city": "Antalya",
"pc": 7160
},
{
"adress": "blabla",
"city": "Adana",
"pc": 1170
}
]
}
```

----------

###Delphi Side

```pascal
// foo
var
X: ISuperObject;
begin
X := SO;
X.S['name'] := 'Onur YILDIZ';
X.B['vip'] := true;
with X.A['telephones'] do
begin
Add('000000000');
Add('111111111111');
end;
X.I['age'] := 24;
X.F['size'] := 1.72;
with X.A['adresses'].O[0] {Auto Create} do
begin
S['adress'] := 'blabla';
S['city'] := 'Antalya';
I['pc'] := 7160;
end;
// or
X.A['adresses'].O[1].S['adress'] := 'blabla';
X.A['adresses'].O[1].S['city'] := 'Adana';
X.A['adresses'].O[1].I['pc'] := 1170;
```
----------
###Super Expressions
```pascal
const
JSON = '{ "o": { '+
' "1234567890": {'+
' "last use date": "2010-10-17T01:23:20",'+
' "create date": "2010-10-17T01:23:20",'+
' "name": "iPhone 8s"'+
' }'+
' },'+
' "Index": 0, '+
' "Data": {"Index2": 1}, '+
' "a": [{'+
' "last use date": "2010-10-17T01:23:20",'+
' "create date": "2010-11-17T01:23:20",'+
' "name": "iPhone 8s",'+
' "arr": [1,2,3] '+
' }, '+
' {'+
' message: "hello"'+
' }]'+
'}';

var
X: ISuperObject;
NewJSon: ISuperObject;
NewArray: ISuperArray;
begin
X := SO(JSON);
ShowMessage( X['o."1234567890"."last use date"'].AsString );
ShowMessage( X['a[Index]."create date"'].AsString );
ShowMessage( X['a[Data.Index2].message'].AsString );
X['a[0].arr'].AsArray.Add('test1');
// -----
NewJSON := X['{a: a[Index], b: a[Data.Index2].message, c: o."1234567890".name, d: 4, e: a[0].arr[2], f: " :) "}'].AsObject;
NewArray := X['[a[Index], a[Data.Index2].message, Data.Index2, Index, 1, "1", "test"]'].AsArray;
end;
```
----------
###Where
```pascal
var
FilterJSON: ISuperObject;
begin
FilterJSON := SO('{ Table: [ '+
' { '+
' Name: "Sakar SHAKIR", ' +
' Sex: "M", ' +
' Size: 1.75 '+
' }, '+
' { '+
' Name: "Bulent ERSOY", ' +
' Sex: "F", ' +
' Size: 1.60 '+
' }, '+
' { '+
' Name: "Cicek ABBAS", ' +
' Sex: "M", ' +
' Size: 1.65 '+
' } '+
' ] '+
'}');
Memo1.Lines.Add(
FilterJSON.A['Table'].Where(function(Arg: IMember): Boolean
begin
with Arg.AsObject do
Result := (S['Sex'] = 'M') and (F['Size'] > 1.60)
end).AsJSON
);
end;
```

***Output***
```json
[
{
"Name":"Sakar SHAKIR",
"Sex":"M",
"Size":1.75
},
{
"Name":"Cicek ABBAS",
"Sex":"M",
"Size":1.65
}
]
```
----------
###Delete
```pascal
var
FilterJSON: ISuperObject;
begin
FilterJSON := SO('{ Table: [ '+
' { '+
' Name: "Sakar SHAKIR", ' +
' Sex: "M", ' +
' Size: 1.75 '+
' }, '+
' { '+
' Name: "Bulent ERSOY", ' +
' Sex: "F", ' +
' Size: 1.60 '+
' }, '+
' { '+
' Name: "Cicek ABBAS", ' +
' Sex: "M", ' +
' Size: 1.65 '+
' } '+
' ] '+
'}');
Memo1.Lines.Add(
FilterJSON.A['Table'].Delete(function(Arg: IMember): Boolean
begin
with Arg.AsObject do
Result := (S['Sex'] = 'M') and (F['Size'] > 1.60)
end).AsJSON
);
end;
```
***Output***
```json
[
{
"Name":"Bulent ERSOY",
"Sex":"F",
"Size":1.6
}
]
```

----------
###Sorting
```pascal
var
X: ISuperObject;
A: ISuperArray;
begin
X := SO('{b:1, a:2, d:4, c:2}');
ShowMessage(X.AsJSON);
X.Sort(function(Left, Right: IMember): Integer begin
Result := CompareText(Left.Name, Right.Name);
end);
ShowMessage(X.AsJSON);

A := SA('[{index:3}, {index:4}, {index:2}, {index:1}]');
ShowMessage(A.AsJSON);
A.Sort(function(Left, Right: IMember): Integer begin
Result := CompareValue(Left.AsObject.I['index'], Right.AsObject.I['index']);
end);
ShowMessage(A.AsJSON);
end;
```
***Output***
```json
{"b":1,"a":2,"d":4,"c":2}
{"a":2,"b":1,"c":2,"d":4}
[{"index":3},{"index":4},{"index":2},{"index":1}]
[{"index":1},{"index":2},{"index":3},{"index":4}]
```
----------
###Variant
```pascal
var
X: ISuperObject;
begin
X := TSuperObject.Create;
X.V['A'] := 1;
X.V['B'] := '2';
X.V['C'] := 1.3;
X.V['D'] := False;
X.V['E'] := Null;
X.V['F'] := Now;
Memo1.Lines.Add(X.AsJSON);
end;
```
***Output***
```json
{
"A": 1,
"B": "2",
"C": 1.3,
"D": false,
"E": null,
"F": "2014-05-03T03:25:05.059"
}
```
----------
###Loops
```pascal
const
JSN = '{ '+
' "adresses": [ '+
' { '+
' "adress": "blabla", '+
' "city": "Antalya", '+
' "pc": 7160 '+
' },'+
' { '+
' "adress": "blabla", '+
' "city": "Adana", '+
' "pc": 1170 '+
' } '+
' ] '+
'}';
var
X, Obj: ISuperObject;
J: Integer;
begin
X := TSuperObject.Create(JSN);
with X.A['adresses'] do
for J := 0 to Lenght -1 do
begin
Obj := O[J];
Obj.First;
while not Obj.EoF do
begin
Memo1.Lines.Add( Obj.CurrentKey + ' = ' + VarToStr(Obj.CurrentValue.AsVariant));
Obj.Next;
end;
Memo1.Lines.Add('------');
end;
end;
```
> **Output**
> adress = blabla
> city = Antalya
> pc = 7160

**Or Enumerator**
```pascal
var
X: ISuperObject;
AMember,
OMember: IMember;
begin
X := TSuperObject.Create(JSN);

for AMember in X.A['adresses'] do
begin
for OMember in AMember.AsObject do
Memo1.Lines.Add(OMember.Name + ' = ' + OMember.ToString);

Memo1.Lines.Add('------');
end;
```
> **Output**
> adress = blabla
> city = Adana
> pc = 1170

----------
###Marshalling
```pascal
type

TTestSet = (ttA, ttB, ttC);

TTestSets = set of TTestSet;

TSubRec = record
A: Integer;
B: String;
end;

TSubObj = class
A: Integer;
B: Integer;
end;

TTest = class // Field, Property Support
private
FB: String;
FSubObj: TSubObj;
FSubRec: TSubRec;
FTestSets: TTestSets;
FH: TDateTime;
FJ: TDate;
FK: TTime;
FList: TObjectList<TSubObj>; // or TList<>; But only object types are supported
public
A: Integer;
B: TTestSet;
C: Boolean;
property D: String read FB write FB;
property E: TSubRec read FSubRec write FSubRec;
property F: TSubObj read FSubObj write FSubObj;
property G: TTestSets read FTestSets write FTestSets;
property H: TDateTime read FH write FH;
property J: TDate read FJ write FJ;
property K: TTime read FK write FK;
property L: TObjectList<TSubObj> read FList write FList;
end;

TTestRec = record // Only Field Support
A: Integer;
B: TTestSet;
C: Boolean;
D: String;
E: TSubRec;
F: TSubObj;
G: TTestSets;
H: TDateTime;
J: TDate;
K: TTime;
L: TObjectList<TSubObj>; // or TList<>; But only object types are supported
end;

implementation
...

var
Parse: TTest; // For Class;
S: String;
begin
Parse := TTest.FromJSON('{"A": 1, "B": 0, "C": true, "D": "Hello", "E":{"A": 3, "B": "Delphi"}, "F": {"A": 4, "B": 5}, "G": [0,2], "H": "2014-05-03T03:25:05.059", "J": "2014-05-03", "K": "03:25:05", "L":[{"A": 4, "B": 5},{"A": 6, "B": 7}] }');
S := Parse.AsJSON;
end;

...
var
Parse: TTestRec; // For Record;
S: String;
begin
Parse := TJSON.Parse<TTestRec>('{"A": 1, "B": 0, "C": true, "D": "Hello", "E":{"A": 3, "B": "Delphi"}, "F": {"A": 4, "B": 5}, "G": [0,2], "H": "2014-05-03T03:25:05.059", "J": "2014-05-03", "K": "03:25:05", "L":[{"A": 4, "B": 5},{"A": 6, "B": 7}]}');
S := TJSON.Stringify<TTestRec>(Parse);
end;

```
```pascal
type
TRec = record // or class
A: Integer;
B: String;
end;

implementation
...

var
Test: TArray<TRec>;
S: String;
begin
Test := TJSON.Parse<TArray<TRec>>('[{"A": 1, "B": "1"}, {"A": 2, "B": "2"}]');
S := TJSON.Stringify<TArray<TRec>>(Test);
end;
```
Collection (Limited)
```pascal
type
TXCollectionItem = class(TCollectionItem)
private
FA: String;
FB: Integer;
public
property A: String read FA write FA;
property B: Integer read FB write FB;
end;

TXCollection = class(TCollection)
public
constructor Create; reintroduce;
end;

TTest = class
private
FCollection: TXColleciton;
public
destructor Destroy; override;
property Collection: TCollection read FCollection write FCollection;
end;

implementation
...

constructor TXCollection.Create;
begin
inherited Create(TXCollectionItem);
end;

destructor TTest.Destroy;
begin
FCollection.Free;
inherited;
end;

var
Test: TTest;
begin
Test := TTest.FromJSON('{"Collection": [{"A": "Item 1", "B": 1}, {"A": "Item 2", "B": 1}]}');
S := Test.AsJSON;
end;
```
----------
###Marshalling **Attributes**
```pascal
TTest = class
public
[ALIAS('Type')]
Typ: String;
[ALIAS('Unit')]
Unt: Integer;
[REVAL('', '*')]
Filter: String;
[DISABLE]
BlaBlaBla: String;
[REVAL(roEmptyArrayToNull)]
Arr: TArray<String>;
end;

var X: Test;
begin
X := TTest.Create;
X.Typ := 'XType';
X.Unt := 2;
X.Filter := '';
X.BlaBlaBla := ':)';
SetLength(X.Arr, 0);
ShowMessage(X.AsJSON);
end;
```
***Output***
```json
{
"Type": "XType",
"Unit": 2,
"Filter": "*",
"Arr": null
}
```

x-superobject的更多相关文章

  1. XE3随笔6:SuperObject 的 JSON 对象中还可以包含 "方法"

    SuperObject 的 JSON 对象中还可以包含 "方法", 这太有意思了; 其方法的格式是: procedure Method(const This, Params: IS ...

  2. XE随想4:SuperObject增、删、改

    unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...

  3. Delphi7下SuperObject的JSON使用方法

    uses superobject; procedure TForm1.FormCreate(Sender: TObject); var aJson: ISuperObject; aSuperArray ...

  4. JSON 之 SuperObject(10): Merge、Clone、ForcePath

    unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...

  5. JSON 之 SuperObject(9): TSuperType

    unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...

  6. JSON 之 SuperObject(8): 关于乱码的几种情况 - 向 Henri Gourvest 大师报告

    这几天学习 JSON - SuperObject, 非常幸运地得到了其作者 Henri Gourvest 大师的同步指点! (Henri 大师也是 DSPack 和 GDI+ 头文件的作者; 大师是法 ...

  7. JSON 之 SuperObject(6): 方法

    SuperObject 的 JSON 对象中还可以包含 "方法", 这太有意思了; 其方法的格式是: procedure Method(const This, Params: IS ...

  8. JSON 之 SuperObject(7): 可以省略的双引号

    在 JSON 中, 字符串应该在双引号中; 从上个例子才发现: 原来这个双引号可以省略, 有空格都行 当然只是在程序代码中可以省略, 对象会自动识别添加的. 即如此, 下面写法都可以: uses Su ...

  9. JSON 之 SuperObject(4): 增、删、改

    unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...

  10. JSON 之 SuperObject(5): Format 与转义字符

    unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...

随机推荐

  1. C++之插入迭代器

    #include<iostream> #include<vector> #include<list> #include<iterator> usingn ...

  2. Ubuntu 16.04 使用docker资料汇总与应用docker安装caffe并使用Classifier(ros kinetic+usb_cam+caffe)

    Docker是开源的应用容器引擎.若想简单了解一下,可以参考百度百科词条Docker.好像只支持64位系统. Docker官网:https://www.docker.com/ Docker - 从入门 ...

  3. Ubuntu 登陆循环启动 无法进入桌面 libGL error: failed to load driver: swrast

    导致无法进入Ubuntu图形界面的原因很多,关键是要找到问题原因所在. 最佳的方法是查看.xsession-errors这个日志文件,依据问题解决~ 这里先汇总一下问题: 1 环境变量导致的,进入tt ...

  4. 如何在VS2013创建WebService并在IIS中发布

    第一步:打开VS2013,选择文件->新建->项目. 第二步:选择[ASP.net 空web应用程序],将其命名为自己想的工程名称. 第三步:然后右键点击工程,添加->web服务.然 ...

  5. Kafka压力测试(自带测试脚本)(单机版)

    一.测试目的 本次性能测试在正式环境下单台服务器上Kafka处理MQ消息能力进行压力测试.测试包括对Kafka写入MQ消息和消费MQ消息进行压力测试,根据10w.100w和1000w级别的消息处理结果 ...

  6. 20155225 2006-2007-2 《Java程序设计》第四周学习总结

    20155225 2006-2007-2 <Java程序设计>第四周学习总结 教材学习内容总结 对"是一种"语法测试几次之后,总结一句:满足"是一种" ...

  7. Elasticsearch 6.x 入门测试

    首先听一下官方的话: https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html 我尝试了使用Java作为Cl ...

  8. IDEA简单部署MVC项目

    本文章主要参考七小栈主的文章,特此说明: 前提 在配置好JDK,配置好Maven,配置链接如下:如何配置Java环境,包括JDK,Maven等 建项目 IEDA中File->New->Pr ...

  9. 最小圆覆盖 gym-102006 I

    #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk mak ...

  10. Linux 常用基本命令及应用技巧

    需要pdf 版 联系我 我的文件中有目录一.Linux 的常用基本命令................................................................. ...