Difference between revisions of "IoTGateway/AdvJSON"
From ESS-WIKI
Fred.chang (talk | contribs) (→Fundamental) |
Fred.chang (talk | contribs) |
||
| (2 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
| − | =What is JSON?= | + | = What is JSON? = |
| + | |||
*JSON stands for JavaScript Object Notation | *JSON stands for JavaScript Object Notation | ||
*JSON is a lightweight data-interchange format | *JSON is a lightweight data-interchange format | ||
| Line 5: | Line 6: | ||
*JSON is "self-describing" and easy to understand | *JSON is "self-describing" and easy to understand | ||
| − | =C functions= | + | = C functions = |
| − | ==List== | + | |
| − | < | + | == List == |
| − | ''Description'' | + | |
| + | |||
| + | <syntaxhighlight lang="C">int JSON_Validator(const char *json);</syntaxhighlight>''Description'' | ||
*This function validates a string in JSON format. | *This function validates a string in JSON format. | ||
| + | |||
''Parameters'' | ''Parameters'' | ||
| − | * json [POINTER] | + | |
| + | *json [POINTER] | ||
| + | |||
::a string. | ::a string. | ||
| + | |||
''Return Values'' | ''Return Values'' | ||
| + | |||
*[0|1] Error or Success. | *[0|1] Error or Success. | ||
| − | + | ||
| + | |||
| + | |||
---- | ---- | ||
| − | <br> | + | |
| − | < | + | |
| − | ''Description'' | + | <br/><syntaxhighlight lang="C">JSONode *JSON_Parser(const char *json);</syntaxhighlight> ''Description'' |
*Translate a string into a tree structure pointer(JSONode); | *Translate a string into a tree structure pointer(JSONode); | ||
| + | |||
''Parameters'' | ''Parameters'' | ||
| − | * json [POINTER] | + | |
| + | *json [POINTER] | ||
| + | |||
::a string. | ::a string. | ||
| + | |||
''Return Values'' | ''Return Values'' | ||
| + | |||
*(JSONode *) | *(JSONode *) | ||
| + | |||
::A pointer describes a JSON tree structure. | ::A pointer describes a JSON tree structure. | ||
| − | + | ||
| + | |||
| + | |||
---- | ---- | ||
| − | <br> | + | |
| − | < | + | |
| − | ''Description'' | + | <br/><syntaxhighlight lang="C">void JSON_Destory(JSONode **json);</syntaxhighlight> ''Description'' |
*Destory a JSON tree sturcture; | *Destory a JSON tree sturcture; | ||
| + | |||
''Parameters'' | ''Parameters'' | ||
| − | * json [POINTER] | + | |
| + | *json [POINTER] | ||
| + | |||
::double-pointer of JSON tree sturcture. | ::double-pointer of JSON tree sturcture. | ||
| + | |||
''Return Values'' | ''Return Values'' | ||
| + | |||
*None | *None | ||
| − | + | ||
| + | |||
| + | |||
---- | ---- | ||
| − | <br> | + | |
| − | < | + | |
| − | ''Description'' | + | <br/><syntaxhighlight lang="C">JSONode *JSON_Copy(JSONode *json);</syntaxhighlight> ''Description'' |
*Copy a JSON tree sturcture into another pointer. | *Copy a JSON tree sturcture into another pointer. | ||
| + | |||
''Parameters'' | ''Parameters'' | ||
| − | * json [POINTER] | + | |
| + | *json [POINTER] | ||
| + | |||
::source pointer | ::source pointer | ||
| + | |||
''Return Values'' | ''Return Values'' | ||
| + | |||
*(JSONode *) | *(JSONode *) | ||
| + | |||
::result pointer | ::result pointer | ||
| − | + | ||
| + | |||
| + | |||
---- | ---- | ||
| − | <br> | + | |
| − | < | + | |
| − | ''Description'' | + | <br/><syntaxhighlight lang="C">void JSON_Get(JSONode *json, const char *path, char *result, int size);</syntaxhighlight> ''Description'' |
*A Structured-Path-Query function. | *A Structured-Path-Query function. | ||
| + | |||
''Parameters'' | ''Parameters'' | ||
| − | * json [POINTER] | + | |
| + | *json [POINTER] | ||
| + | |||
::source. | ::source. | ||
| + | |||
*path [POINTER] | *path [POINTER] | ||
| + | |||
::a structure-path string | ::a structure-path string | ||
| + | |||
*result [POINTER] | *result [POINTER] | ||
| + | |||
::result string | ::result string | ||
| + | |||
*size [NUMBER] | *size [NUMBER] | ||
| + | |||
::the size of result. | ::the size of result. | ||
| + | |||
''Return Values'' | ''Return Values'' | ||
| + | |||
*None | *None | ||
| + | |||
''Example'' | ''Example'' | ||
| − | < | + | <syntaxhighlight lang="C"> |
char result[256]; | char result[256]; | ||
JSON_Get(json, "[a][b][c][d]", result, 256); | JSON_Get(json, "[a][b][c][d]", result, 256); | ||
JSON_Get(json, "/a/b/c/d", result, 256); | JSON_Get(json, "/a/b/c/d", result, 256); | ||
| − | </ | + | </syntaxhighlight><br/> |
| − | <br> | ||
---- | ---- | ||
| − | <br> | + | |
| − | < | + | |
| − | ''Description'' | + | <br/><syntaxhighlight lang="C">void JSON_Cmd(JSONode *json, const char *path, char *assign, int size);</syntaxhighlight> ''Description'' |
*A Command-line-like function. | *A Command-line-like function. | ||
| + | |||
''Parameters'' | ''Parameters'' | ||
| − | * json [POINTER] | + | |
| + | *json [POINTER] | ||
| + | |||
::source. | ::source. | ||
| + | |||
*path [POINTER] | *path [POINTER] | ||
| + | |||
::a command string | ::a command string | ||
| + | :::*Get or GET: for getting the value of element | ||
| + | :::*New or POST: for creating a new element | ||
| + | :::*Edit or PUT: for editing the value of element | ||
| + | :::*Delete or DELETE: for deleting a element | ||
| + | :::P.S. All upper case is REST style. | ||
*assign[POINTER] | *assign[POINTER] | ||
| + | |||
::target | ::target | ||
| + | |||
*size [NUMBER] | *size [NUMBER] | ||
| + | |||
::the size of target. | ::the size of target. | ||
| + | |||
''Return Values'' | ''Return Values'' | ||
| + | |||
*None | *None | ||
| + | |||
''Example'' | ''Example'' | ||
| − | < | + | <syntaxhighlight lang="C"> |
JSON_Cmd(json, "New [a][b][c][d]", "123", 3); | JSON_Cmd(json, "New [a][b][c][d]", "123", 3); | ||
JSON_Cmd(json, "POST /a/b/c/d", "123", 3); | JSON_Cmd(json, "POST /a/b/c/d", "123", 3); | ||
| − | </ | + | </syntaxhighlight> |
| + | = C++ = | ||
| + | |||
| + | == Fundamental == | ||
| + | |||
| + | *Read only | ||
| − | |||
| − | |||
| − | |||
::name = movie[“name”].String(); | ::name = movie[“name”].String(); | ||
| − | ::<font color=red>movie[“name”] = “After Tomorrow”; </font> | + | ::<font color="red">movie[“name”] = “After Tomorrow”; </font> <font color="green">//Error, Read only</font> |
| − | * Edit | + | |
| − | ::movie.Edit()[“name”] = “After Tomorrow”; | + | *Edit |
| − | ::movie.Edit()[“name”] << “Name”; | + | |
| − | ::<font color=red>movie.Edit()[“Year”] = 2004; </font> | + | ::movie.Edit()[“name”] = “After Tomorrow”; <font color="green">//Edit 'Value'</font> |
| − | * New | + | ::movie.Edit()[“name”] << “Name”; <font color="green">//Edit 'Key'</font> |
| − | ::movie.New()[“Year”] = 2004; <font color=green>//Create a new element</font> | + | ::<font color="red">movie.Edit()[“Year”] = 2004; </font> <font color="green">//Error, Can't create new element</font> |
| − | * Erase | + | |
| + | *New | ||
| + | |||
| + | ::movie.New()[“Year”] = 2004; <font color="green">//Create a new element</font> | ||
| + | |||
| + | *Erase | ||
| + | |||
::movie[“Year”].Erase(); | ::movie[“Year”].Erase(); | ||
| − | * Delete | + | |
| + | *Delete | ||
| + | |||
::movie[“Year”].Delete(); | ::movie[“Year”].Delete(); | ||
| − | ==Erase & Delete== | + | == Erase & Delete == |
| + | |||
| + | <big>'''Example:'''</big><br/>AdvJSON json("{}");<br/>json.New()["number"] = {1,2,3,4,5,6,7,8,9,10};<br/>'''result:''' | ||
| − | |||
| − | |||
| − | |||
| − | |||
{"number":[1,2,3,4,5,6,7,8,9,10]} | {"number":[1,2,3,4,5,6,7,8,9,10]} | ||
*Erase | *Erase | ||
| − | json["number"][5].Erase();<br> | + | |
| − | '''result:''' | + | json["number"][5].Erase();<br/>'''result:''' |
| + | |||
{"number":[1,2,3,4,5,null,7,8,9,10]} | {"number":[1,2,3,4,5,null,7,8,9,10]} | ||
*Delete | *Delete | ||
| − | json["number"][3].Delete();<br> | + | |
| − | '''result:''' | + | json["number"][3].Delete();<br/>'''result:''' |
| + | |||
{"number":[1,2,3,5,null,7,8,9,10]} | {"number":[1,2,3,5,null,7,8,9,10]} | ||
| − | ==Operation== | + | == Operation == |
| − | ===Indicator=== | + | |
| − | Using Array operator to access the JSON tree.<br> | + | === Indicator === |
| − | <big>'''Example:'''</big> | + | |
| + | Using Array operator to access the JSON tree.<br/><big>'''Example:'''</big> | ||
| + | |||
movie[“format”][“type”] | movie[“format”][“type”] | ||
movie[“format”][“frame rate”][0] | movie[“format”][“frame rate”][0] | ||
| − | ===Array=== | + | === Array === |
| + | |||
<big>'''Example:'''</big> | <big>'''Example:'''</big> | ||
| + | |||
movie.Edit()[“format”][“frame rate”] = {8,16,32}; | movie.Edit()[“format”][“frame rate”] = {8,16,32}; | ||
movie.New()[“format”][“frame rate”] += {64,128,256}; | movie.New()[“format”][“frame rate”] += {64,128,256}; | ||
movie.New()[“format”][“resolution”] = { {720,480}, {1920,1080}, {4096,2160} }; | movie.New()[“format”][“resolution”] = { {720,480}, {1920,1080}, {4096,2160} }; | ||
| − | ===Parallel=== | + | === Parallel === |
| − | [<font color=red>{ , , , }</font>] = <font color=red>{ , , , }</font><br> | + | |
| − | <big>'''Example:'''</big><br> | + | [<font color="red">{ , , , }</font>] = <font color="red">{ , , , }</font><br/><big>'''Example:'''</big><br/>AdvJSON movie("{}"); |
| − | AdvJSON movie("{}"); | + | |
movie.New()[{"name", "width", "height", "frame rate"}] = | movie.New()[{"name", "width", "height", "frame rate"}] = | ||
{"Jurassic World", 1920, 1080, {15, 25, 30}}; | {"Jurassic World", 1920, 1080, {15, 25, 30}}; | ||
| + | |||
'''Result:''' | '''Result:''' | ||
| + | |||
{ | { | ||
"name": "Jurassic World", | "name": "Jurassic World", | ||
| Line 157: | Line 233: | ||
} | } | ||
| − | ==Creator== | + | == Creator == |
| − | C[ <font color=red>{ , , , }</font> ]( <font color=red>{ , , , }</font> )<br> | + | |
| − | <big>'''Example:'''</big> | + | C[ <font color="red">{ , , , }</font> ]( <font color="red">{ , , , }</font> )<br/><big>'''Example:'''</big> |
| + | |||
AdvJSON movie("{}"); | AdvJSON movie("{}"); | ||
movie.New()[{"name", "width", "height", "frame rate"}] = | movie.New()[{"name", "width", "height", "frame rate"}] = | ||
| − | {"Jurassic World", 1920, 1080, | + | {"Jurassic World", 1920, 1080, {15, 25, 30}}; |
| + | |||
| + | movie.New()["info"] = C[{ "video" , "audio" }]({ | ||
| + | {"H264","6000"} , {"AAC", "14400"}}); | ||
| − | |||
'''Result:''' | '''Result:''' | ||
| + | |||
{ | { | ||
"name": "Jurassic World", | "name": "Jurassic World", | ||
| Line 177: | Line 257: | ||
} | } | ||
| − | ==Example== | + | == Example == |
| + | |||
AdvJSON iot("{}"); | AdvJSON iot("{}"); | ||
AdvJSONCreator C(iot); | AdvJSONCreator C(iot); | ||
| Line 195: | Line 276: | ||
1 | 1 | ||
}; | }; | ||
| + | |||
'''Result:''' | '''Result:''' | ||
| + | |||
{ | { | ||
"IoTGW": { | "IoTGW": { | ||
Latest revision as of 03:17, 19 November 2018
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);
- 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);
- 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);
- Destory a JSON tree sturcture;
Parameters
- json [POINTER]
- double-pointer of JSON tree sturcture.
Return Values
- None
JSONode *JSON_Copy(JSONode *json);
- 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);
- 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);
- A Command-line-like function.
Parameters
- json [POINTER]
- source.
- path [POINTER]
- a command string
- Get or GET: for getting the value of element
- New or POST: for creating a new element
- Edit or PUT: for editing the value of element
- Delete or DELETE: for deleting a element
- P.S. All upper case is REST style.
- 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
Using Array operator to access the JSON tree.
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"]
}
}
Example
AdvJSON iot("{}");
AdvJSONCreator C(iot);
iot.New()["IoTGW"]["WSN"]["WSN0"][{"Info","bn","ver"}] = {
C[{"e","bn"}]({
{
C[{"n","sv","asm"}]({"SenHubList","0000000EC6F0F830,0000000EC6F0F831","r"}),
C[{"n","sv","asm"}]({"Neighbor","0000000EC6F0F830","r"}),
C[{"n","v","asm"}]({"Health",80,"r"}),
C[{"n","sv","asm"}]({"Name","WSN0","r"}),
C[{"n","sv","asm"}]({"sw","1.0.0.1","r"}),
C[{"n","bv","asm"}]({"reset",0,"rw"}),
},
"Info"
}),
"0000852CF4B7B0E8",
1
};
Result:
{
"IoTGW": {
"WSN: {
"WSN0": {
"Info": {
"e": [
{ "n": "SenHubList", "sv": "0000000EC6F0F830,0000000EC6F0F831", "asm": "r"},
{ "n": "Neighbor", "sv": "0000000EC6F0F830", "asm": "r"},
{ "n": "Health", "v": 80, "asm": "r"},
{ "n": "Name", "sv": "WSN0", "asm": "r"},
{ "n": "sw", "sv": "1.0.0.1", "asm": "r"},
{ "n": "reset", "bv": 0, "asm": "rw"},
],
"bn": "Info"
},
"bn": "0000852CF4B7B0E8",
"ver": 1
}
}
}
}