Difference between revisions of "IoTGateway/AdvJSON"
Fred.chang (talk | contribs) (→Creator) |
Fred.chang (talk | contribs) (→Creator) |
||
Line 164: | Line 164: | ||
==Creator== | ==Creator== | ||
− | [ { , , , } ]( { , , , } ) | + | C[ <font color=red>{ , , , }</font> ]( <font color=red>{ , , , }</font> )<br> |
+ | <big>'''Example:'''</big><br> | ||
+ | AdvJSON movie("{}"); | ||
+ | movie.New()[{"name", "width", "height", "frame rate"}] = | ||
+ | {"Jurassic World", 1920, 1080, {15, 25, 30}}; | ||
+ | |||
+ | movie.New()["info"] = C[{"video","audio"}]({{"H264","6000"} , {"AAC", "14400"}}); | ||
+ | '''Result:''' | ||
+ | { | ||
+ | "name": "Jurassic World", | ||
+ | "width": 1920, | ||
+ | "height": 1080, | ||
+ | "frame rate": {15, 25, 30} | ||
+ | "info": { | ||
+ | "video": ["H264","6000"], | ||
+ | "audio": ["AAC", "14400"] | ||
+ | } | ||
+ | } | ||
==Example== | ==Example== |
Revision as of 03:42, 24 February 2016
Contents
What is JSON?
- JSON stands for JavaScript Object Notation
- JSON is a lightweight data-interchange format
- JSON is language independent
- JSON is "self-describing" and easy to understand
C functions
List
int JSON_Validator(const char *json);
Description
- This function validates a string in JSON format.
Parameters
- json [POINTER]
- a string.
Return Values
- [0|1] Error or Success.
JSONode *JSON_Parser(const char *json);
Description
- Translate a string into a tree structure pointer(JSONode);
Parameters
- json [POINTER]
- a string.
Return Values
- (JSONode *)
- A pointer describes a JSON tree structure.
void JSON_Destory(JSONode **json);
Description
- Destory a JSON tree sturcture;
Parameters
- json [POINTER]
- double-pointer of JSON tree sturcture.
Return Values
- None
JSONode *JSON_Copy(JSONode *json);
Description
- Copy a JSON tree sturcture into another pointer.
Parameters
- json [POINTER]
- source pointer
Return Values
- (JSONode *)
- result pointer
void JSON_Get(JSONode *json, const char *path, char *result, int size);
Description
- A Structured-Path-Query function.
Parameters
- json [POINTER]
- source.
- path [POINTER]
- a structure-path string
- result [POINTER]
- result string
- size [NUMBER]
- the size of result.
Return Values
- None
Example
char result[256];
JSON_Get(json, "[a][b][c][d]", result, 256);
JSON_Get(json, "/a/b/c/d", result, 256);
void JSON_Cmd(JSONode *json, const char *path, char *assign, int size);
Description
- A Command-line-like function.
Parameters
- json [POINTER]
- source.
- path [POINTER]
- a command string
- assign[POINTER]
- target
- size [NUMBER]
- the size of target.
Return Values
- None
Example
JSON_Cmd(json, "New [a][b][c][d]", "123", 3);
JSON_Cmd(json, "POST /a/b/c/d", "123", 3);
C++
Fundamental
- Read only
name = movie[“name”].String();
movie[“name”] = “After Tomorrow”; //Error, Read only
- Edit
movie.Edit()[“name”] = “After Tomorrow”; //Edit 'Value'
movie.Edit()[“name”] << “Name”; //Edit 'Key'
movie.Edit()[“Year”] = 2004; //Error, Can't create new element
- New
movie.New()[“Year”] = 2004; //Create a new element
- Erase
movie[“Year”].Erase();
- Delete
movie[“Year”].Delete();
Erase & Delete
Example:
AdvJSON json("{}");
json.New()["number"] = {1,2,3,4,5,6,7,8,9,10};
result:
{"number":[1,2,3,4,5,6,7,8,9,10]}
- Erase
json["number"][5].Erase();
result:
{"number":[1,2,3,4,5,null,7,8,9,10]}
- Delete
json["number"][3].Delete();
result:
{"number":[1,2,3,5,null,7,8,9,10]}
Operation
Indicator
Example:
movie[“format”][“type”] movie[“format”][“frame rate”][0]
Array
Example:
movie.Edit()[“format”][“frame rate”] = {8,16,32}; movie.New()[“format”][“frame rate”] += {64,128,256}; movie.New()[“format”][“resolution”] = { {720,480}, {1920,1080}, {4096,2160} };
Parallel
[{ , , , }] = { , , , }
Example:
AdvJSON movie("{}");
movie.New()[{"name", "width", "height", "frame rate"}] = {"Jurassic World", 1920, 1080, {15, 25, 30}};
Result:
{ "name": "Jurassic World", "width": 1920, "height": 1080, "frame rate": {15, 25, 30} }
Creator
C[ { , , , } ]( { , , , } )
Example:
AdvJSON movie("{}"); movie.New()[{"name", "width", "height", "frame rate"}] = {"Jurassic World", 1920, 1080, {15, 25, 30}};
movie.New()["info"] = C[{"video","audio"}]({{"H264","6000"} , {"AAC", "14400"}});
Result:
{ "name": "Jurassic World", "width": 1920, "height": 1080, "frame rate": {15, 25, 30} "info": { "video": ["H264","6000"], "audio": ["AAC", "14400"] } }