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. Ubuntu CEPH快速安装

    一.CEPH简介 不管你是想为云平台提供Ceph 对象存储和/或 Ceph 块设备,还是想部署一个 Ceph 文件系统或者把 Ceph 作为他用,所有 Ceph 存储集群的部署都始于部署一个个 Cep ...

  2. Java测试框架Mockito源码分析

    1.Mockito简介 测试驱动的开发(Test Driven Design, TDD)要求我们先写单元测试,再写实现代码.在写单元测试的过程中,一个很普遍的问题是,要测试的类会有很多依赖,这些依赖的 ...

  3. more命令 less命令

    more命令是一个基于vi编辑器文本过滤器,它以全屏幕的方式按页显示文本文件的内容,支持vi中的关键字定位操作.more名单中内置了若干快捷键,常用的有H(获得帮助信息),Enter(向下翻滚一行), ...

  4. java8 - Optional

    mport java.util.Optional; import org.junit.Test; /* * 一.Optional 容器类:用于尽量避免空指针异常 * Optional.of(T t) ...

  5. Rookey.Frame之实体类

    上周跟大家分享了Rookey.Frame框架的初始化功能,今天继续给大家介绍实体类的设计. 先看下下面菜单实体示例代码: using Rookey.Frame.EntityBase; using Ro ...

  6. slf4j logback pom

    pom: <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding&g ...

  7. 百度地图API--Key的获得

    [开年后花了半个月的时间学习了百度地图API开发,准备投入项目中,学习的过程中写了一些简单的总结,在部门内部做了一个简单的分享培训,这里希望将自己的仅有的一点点关于百度地图API的收获分享给社区,整个 ...

  8. JavaScript基础-DAY2

    JavaScript对象 在JavaScript中除了null和undefined以外其他的数据类型都被定义成了对象,也可以用创建对象的方法定义变量,String.Math.Array.Date.Re ...

  9. python实现括号匹配

    1.用一个栈[python中可以用List]就可以解决,时间和空间复杂度都是O(n) # -*- coding: utf8 -*- # 符号表 SYMBOLS = {'}': '{', ']': '[ ...

  10. jQuery学习总结2

    六.动画效果 6.1.基本 hide([speed,[fn]])隐藏显示的元素 speed: 三种预定速度之一的字符串("slow","normal", or ...