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的更多相关文章

  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. Struts 2 - Hello World Example

    As you learnt from the Struts 2 architecture, when you click on a hyperlink or submit an HTML form i ...

  2. CF2B The least round way 题解

    都是泪呀...↑ 题目传送门 题意(直接复制了QWQ) 题目描述 给定由非负整数组成的\(n \times n\)的正方形矩阵,你需要寻找一条路径: 以左上角为起点, 每次只能向右或向下走, 以右下角 ...

  3. Java学习(正则表达式、Date类、DateFormat类、Calendar类)

    一.正则表达式 1.概念:英语:Regular Expression,在代码中常简写为regex.正则表达式,是一个字符串,使用单个字符串来描述.用来定义匹配规则,匹配一系列符合某个句法规则的字符串. ...

  4. linux 101 hacks 2date,grep,find

    感觉挨个按着作者来的太蠢了,我还是放自己觉得不错的东西把 用特定格式显示当前时间 以下的方法可以用各种不同的格式来显示当前时间: $ date Thu Jan :: PST $ date --date ...

  5. MFC+WinPcap编写一个嗅探器之四(获取模块)

    这一节主要介绍如何获取设备列表,比较简单 获取设备列表主要是在CAdpDlg中完成,也就是对应之前创建的选择适配器模块,如图: 当打开选择适配器对话框后,在列表视图控件中显示当前主机所有适配器及适配器 ...

  6. Gitlab服务器维护

    一. 内容 Gitlab服务器的更新 Gitlab服务器备份与恢复 导入Git仓库 二. Gitlab服务器的更新 1. 使用SSH登陆Gitlab服务器 2. 停止后端的unicorn服务器 [ro ...

  7. hdoj2191 珍惜现在,感恩生活(01背包 || 多重背包)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2191 思路 由于每种大米可能不止一袋,所以是多重背包问题,可以直接使用解决多重背包问题的方法,也可以将 ...

  8. (转)python的range()函数用法

    使用python的人都知道range()函数很方便,今天再用到他的时候发现了很多以前看到过但是忘记的细节.这里记录一下range(),复习下list的slide,最后分析一个好玩儿的冒泡程序. 转自: ...

  9. JAVA 9 新特性

     Oracle已将JAVA 9的开发提上日程.OpenJDK上已经出现了关于下一个主版本JAVA 9的改进建议(JEP).与以往不同,Oracle在这次谈及了一些真正的特性.而早期对于JDK9的声明仅 ...

  10. HTTP/FTP压力测试工具siege

    HTTP/FTP压力测试工具siege   压力测试可以检测服务器的承载能力.针对HTTP和FTP服务,Kali Linux提供专项工具siege.该工具可以模拟多个用户同时访问同一个网站的多个网页, ...