superobject
GITHUB:
https://github.com/hgourvest/superobject
# SuperObject
## What is JSON ?
- JSON (JavaScript Object Notation) is a lightweight data-interchange format.
- It is easy for humans to read and write.
- It is easy for machines to parse and generate.
- It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999.
- JSON is a text format that is completely language independent but uses conventions that are familiar to programmers.
- These properties make JSON an ideal data-interchange language.
- You can get more informations on [json.org](http://www.json.org).
```js
{
"name": "Jon Snow", /* this is a comment */
"dead": true,
"telephones": ["000000000", "111111111111"],
"age": 33,
"size": 1.83,
"adresses": [
{
"adress": "foo",
"city": "The wall",
"pc": 57000
},
{
"adress": "foo",
"city": "Winterfell",
"pc": 44000
}
]
}
```
## Parsing a JSON data structure
```pas
var
obj: ISuperObject;
begin
obj := SO('{"foo": true}');
obj := TSuperObject.ParseString('{"foo": true}');
obj := TSuperObject.ParseStream(stream);
obj := TSuperObject.ParseFile(FileName);
end;
```
## Accessing data
There isn't individual datastructure for each supported data types.
They are all an object: the ISuperObject.
```pas
val := obj.AsString;
val := obj.AsInteger;
val := obj.AsBoolean;
val := obj.AsDouble;
val := obj.AsArray;
val := obj.AsObject;
val := obj.AsMethod;
```
## How to read a property value of an object ?
```pas
val := obj.AsObject.S['foo']; // get a string
val := obj.AsObject.I['foo']; // get an Int64
val := obj.AsObject.B['foo']; // get a Boolean
val := obj.AsObject.D['foo']; // get a Double
val := obj.AsObject.O['foo']; // get an Object (default)
val := obj.AsObject.M['foo']; // get a Method
val := obj.AsObject.N['foo']; // get a null object
```
## How to read a value from an array ?
```pas
// the advanced way
val := obj.AsArray.S[0]; // get a string
val := obj.AsArray.I[0]; // get an Int64
val := obj.AsArray.B[0]; // get a Boolean
val := obj.AsArray.D[0]; // get a Double
val := obj.AsArray.O[0]; // get an Object (default)
val := obj.AsArray.M[0]; // get a Method
val := obj.AsArray.N[0]; // get a null object
```
## Using paths
Using paths is a very productive method to find an object when you know where is it.
This is some usage cases:
```pas
obj['foo']; // get a property
obj['123']; // get an item array
obj['foo.list']; // get a property from an object
obj['foo[123]']; // get an item array from an object
obj['foo(1,2,3)']; // call a method
obj['foo[]'] := value; // add an item array
```
you also can encapsulate paths:
```pas
obj := so('{"index": 1, "items": ["item 1", "item 2", "item 3"]}');
obj['items[index]'] // return "item 2"
```
or recreate a new data structure from another:
```pas
obj := so('{"index": 1, "items": ["item 1", "item 2", "item 3"]}');
obj['{"item": items[index], "index": index}'] // return {"item": "item 2", "index": 1}
```
## Browsing data structure
### Using Delphi enumerator.
Using Delphi enumerator you can browse item's array or property's object value in the same maner.
```pas
var
item: ISuperObject;
begin
for item in obj['items'] do ...
```
you can also browse the keys and values of an object like this:
```pas
var
item: TSuperAvlEntry;
begin
for item in obj.AsObject do ...
begin
item.Name;
item.Value;
end;
```
### Browsing object properties without enumerator
```pas
var
item: TSuperObjectIter;
begin
if ObjectFindFirst(obj, item) then
repeat
item.key;
item.val;
until not ObjectFindNext(item);
ObjectFindClose(item);
```
### Browsing array items without enumerator
```pas
var
item: Integer;
begin
for item := 0 to obj.AsArray.Length - 1 do
obj.AsArray[item]
```
## RTTI & marshalling in Delphi 2010
```pas
type
TData = record
str: string;
int: Integer;
bool: Boolean;
flt: Double;
end;
var
ctx: TSuperRttiContext;
data: TData;
obj: ISuperObject;
begin
ctx := TSuperRttiContext.Create;
try
data := ctx.AsType<TData>(SO('{str: "foo", int: 123, bool: true, flt: 1.23}'));
obj := ctx.AsJson<TData>(data);
finally
ctx.Free;
end;
end;
```
## Saving data
```pas
obj.AsJSon(options);
obj.SaveTo(stream);
obj.SaveTo(filename);
```
## Helpers
```pas
SO(['prop1', true, 'prop2', 123]); // return an object {"prop1": true, "prop2": 123}
SA([true, 123]); // return an array [true, 123]
```
## Non canonical forms
The SuperObject is able to parse non canonical forms.
```pas
// unquoted identifiers
SO('{foo: true}');
// unescaped or unquoted strings
SO('{собственность: bla bla bla}');
// excadecimal
SO('{foo: \xFF}');
```
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 ...
随机推荐
- Knockout应用开发指南 应用举例(简单、高级)
Knockout应用开发指南 第八章:简单应用举例(1)http://www.cnblogs.com/TomXu/archive/2011/11/30/2257067.htmlKnockout应用开发 ...
- 【考古向翻译】Pwn2Own 2010 Windows 7 Internet Explorer 8 exploit
正好在Google搜到了这篇文章,就打算自己翻译一下,也不清楚国内是否有人已经翻译过了.作者是Pwn2Own 2010的获奖者来自荷兰的皮特·维莱格登希尔(Peter Vreugdenhil). 20 ...
- 20165333 学习基础和C语言学习基础
说实话,我并没有什么技能比90%以上的人更好,非要拿一个出来的话,篮球勉强好一点吧.最初接触篮球是小学的时候跟着哥哥看他打球,哥哥的球技在同龄人中算是好的,每次看他各种突破过人,我都觉得特别潇洒帅气, ...
- day5模块学习--sys模块
sys模块 sys模块是处理与系统相关的模块,sys(system),下面来看看sys模块常用的方法: 1.sys.argv #命令行参数list,第一个元素是程序本身路径 2.sys ...
- linux虚拟机磁盘不够用以及进行扩容时遇到的问题
我使用的是:gparted live cd工具 系统是centOS6.2 使用gparted live cd工具进行无损分区,方法很简单,下载iso文件都在VMware对应的linux系统上设置CD ...
- 解析文本文件 "r" 与 "rb" 模式的区别(Python)
r,rb 那么在读文件时,有无b标识的的主要区别在哪里呢? 1.文件使用方式标识 'r':默认值,表示从文件读取数据.'b':表示要读写二进制数据 2.读文件 进行读文件操作时,直到读到文档结束符(E ...
- 自动化CI构建工具
hudson/maven jenkins,bamboo, hudson
- atomikos + druid 连接超时失效
atomikos + druid 连接超时失效,需要多次连接才能成功. 首次连接会报异常: 2018-01-08 16:58:12 DEBUG [com.jpcar.model.dao.jpcar.A ...
- Gitlab-system-hooks
当创建或者删除,用户或者项目时,可能想收到一个通知.Gitlab支持这种类型的system hooks. 下面事件可以触发一个system webhook调用. Project created Pro ...
- PostgreSQL 入门
1.连接数设置为:-1.表示链接数不受限制,理论上可以使用无数个链接. 2.使用外键约束,外键用来在两个表的数据之间建立连接,一个表的外键可以为空值,若不为空值,则每一个外键值必须等于另一个表中主键的 ...