Skip to content

Commit a2d04c8

Browse files
committed
Updated the example and documentation
1 parent e927470 commit a2d04c8

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# json-plus
2-
JSON parser and encoder.
2+
JSON parser and encoder with UTF8 support.
33

44
You do not need to compile anything to use json-plus, just include **`json-plus.cpp`** and **`json-plus.h`** in your application.
55
Everything is contained in the **`json_plus`** namespace.

docs/JSON_Parse.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# JSON_Parse
2+
3+
**json_plus::JSON_Parse(json, context)**
4+
5+
Parses a JSON string.
6+
7+
***json***
8+
The JSON string to parse. This is a `const CHAR*` string type that can be UTF8 encoded.
9+
10+
***context***
11+
Pointer to a `JSON_PARSER_CONTEXT` structure that receives the parsing info.
12+
13+
**Return Value**
14+
A `JSON_NODE` pointer that is the 1st node in the JSON node tree.
15+
16+
**Remarks**
17+
This function will return the JSON node tree built if an error is encountered, so its important to free the node tree even if parsing fails. A simple example would look like this:
18+
```
19+
JSON_PARSER_CONTEXT context;
20+
const CHAR* json_string = "{ \"password\":1234\" }";
21+
JSON_OBJECT json_file = JSON_Parse(json_string, &context);
22+
if (context.errorCode != JSON_ERROR_CODE::NONE) {
23+
JSON_Free(json_file);
24+
printf("%s\n", context.errorDescription);
25+
return -1;
26+
}
27+
```

example.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ int wmain()
8383

8484
// Parse the json string
8585
JSON_OBJECT json_file = JSON_Parse(json_string, &context);
86+
if (context.errorCode != JSON_ERROR_CODE::NONE) {
87+
JSON_Free(json_file);
88+
printf("%s\n", context.errorDescription);
89+
return -1;
90+
}
8691

8792
// Check if the object exists
8893
if (!json_file.Empty())
@@ -118,4 +123,7 @@ int wmain()
118123

119124
// Free json string
120125
free(json_string);
126+
127+
// Exit
128+
return 0;
121129
}

0 commit comments

Comments
 (0)