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 ...
随机推荐
- 四B象限图
- SQL2008关于权限的解释
在SQL2008中我自己创建的一个登录名,可是那个登录名只可以用来登录,对数据库的操作什么都不能,连读取数据库都不可以.因为权限不够,只要把登录名的属性打开点击“服务器角色”,把public和sysa ...
- InterSystems Ensemble学习笔记(二) Ensemble创建镜像, 实现自动故障转移
系列目录 InterSystems Ensemble学习笔记(一) Ensemble介绍及安装InterSystems Ensemble学习笔记(二) Ensemble创建镜像, 实现自动故障转移 一 ...
- sql中循环插入
#!/bin/sh for i in {1..10}dokdsql sys/kdb<<EOF insert into test values(2) ; EOFdone
- Java去重字符串的两种方法以及java中冒号的使用
package com.removesamestring; import java.io.BufferedWriter; import java.util.ArrayList; import java ...
- WCF服务发布到IIS中去(VS2013+win7系统)
第一个WCF程序 1. 新建立空白解决方案,并在解决方案中新建项目,项目类型为:WCF服务应用程序.建立完成后如下图所示: 2.删除系统生成的两个文件IService1.cs与Service1.svc ...
- MySQL服务器 IO 100%的案例分析
[问题] 有台MySQL 5.6.21的数据库实例以写入为主,IO %util接近100% 写入IOPS很高 [分析过程] 1.通过iotop工具可以看到当前IO消耗最高的mysql线程 2.查看线程 ...
- Sockets CF732E set map
题目:http://codeforces.com/problemset/problem/732/E 题目大意: 有n台电脑(computer)和m个插座(socket),两者均有一个power值,分别 ...
- Django框架(一)-Django初识
Django初识 一.Web框架本质—自己实现Web框架 1.所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端 import socket sk = sock ...
- C# 集合类-接口
所谓,程序=数据结构+算法. 我目前的日常工作就是繁琐的业务流程和增删改查之类的. 其实繁琐的业务流程也不过是改变一下数据的状态.怪不得叫,面向数据库编程.哈哈. 所以呢,了解一下各种 .net内置的 ...