x-superobject
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的更多相关文章
- XE3随笔6:SuperObject 的 JSON 对象中还可以包含 "方法"
SuperObject 的 JSON 对象中还可以包含 "方法", 这太有意思了; 其方法的格式是: procedure Method(const This, Params: IS ...
- XE随想4:SuperObject增、删、改
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...
- Delphi7下SuperObject的JSON使用方法
uses superobject; procedure TForm1.FormCreate(Sender: TObject); var aJson: ISuperObject; aSuperArray ...
- JSON 之 SuperObject(10): Merge、Clone、ForcePath
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...
- JSON 之 SuperObject(9): TSuperType
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...
- JSON 之 SuperObject(8): 关于乱码的几种情况 - 向 Henri Gourvest 大师报告
这几天学习 JSON - SuperObject, 非常幸运地得到了其作者 Henri Gourvest 大师的同步指点! (Henri 大师也是 DSPack 和 GDI+ 头文件的作者; 大师是法 ...
- JSON 之 SuperObject(6): 方法
SuperObject 的 JSON 对象中还可以包含 "方法", 这太有意思了; 其方法的格式是: procedure Method(const This, Params: IS ...
- JSON 之 SuperObject(7): 可以省略的双引号
在 JSON 中, 字符串应该在双引号中; 从上个例子才发现: 原来这个双引号可以省略, 有空格都行 当然只是在程序代码中可以省略, 对象会自动识别添加的. 即如此, 下面写法都可以: uses Su ...
- JSON 之 SuperObject(4): 增、删、改
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...
- JSON 之 SuperObject(5): Format 与转义字符
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...
随机推荐
- MySQL学习笔记:regexp正则表达式
在MySQL中,模糊搜索的时候主要用两种方式: 1.like2.regexp + 正则表达式 性能优于like 正则表达式描述了一组字符串. 最简单的正则表达式是不含任何特殊字符的正则表达式.例如,正 ...
- 如何解决vuex因浏览器刷新数据消失,保持数据持久化问题?
vuex的一个全局状态管理的插件,但是在浏览器刷新的时候,内存中的state会释放.通常的解决办法就是用本地存储的方式保存数据,然后再vuex初始化的时候再赋值给state,此过程有点麻烦.因此可以使 ...
- Rookey.Frame之DAL工厂
昨天给大家介绍了表单验证功能,今天给大家介绍下Rookey.Frame框架的数据层工厂,由于Rookey.Frame框架ORM是基于servicestack.ormlite,很多朋友反映这个网上中文资 ...
- loadrunner日志信息
日志分两种1.在VUGEN中运行后的日志2.在controller中运行后的日志 日志设置分两步:1.首先,在VUGEN或controller中run-time setting, 选中always s ...
- CentOS 7下安装Python3.6和pip
一.安装python3.6 1.1.安装python3.6需要依赖包 yum install openssl-devel bzip2-devel expat-devel gdbm-devel read ...
- Docker简介与安装(一)
Docker简介 Docker 是 Docker.Inc 公司开源的一个基于 LXC技术之上构建的Container容器引擎, 源代码托管在 GitHub 上, 基于Go语言并遵从Apache2.0协 ...
- jenkins发邮件问题
1.发送邮件不成功 Sending e-mails to: *******@**.cn Finished: FAILURE 解决办法 jenkins-->configure glo ...
- 8-10 Coping Books uva714
题意:把一个包含m个正整数的序列划分为k个 1<=k<=m<=500的非空连续子序列 使得每个正整数恰好属于一个序列 设第i个序列的各个数之和为 Si 你的任务是让所有的 ...
- Ionic Js十五:对话框
$ionicPopup ionic 对话框服务允许程序创建.显示弹出窗口. $ionicPopup 提供了3个方法:alert(), prompt(),以及 confirm() . 实例 HTML 代 ...
- 基于spring-boot的应用程序的单元测试方案
概述 本文主要介绍如何对基于spring-boot的web应用编写单元测试.集成测试的代码. 此类应用的架构图一般如下所示: 我们项目的程序,对应到上图中的web应用部分.这部分一般分为Control ...