Skip to content

Commit cdb51ac

Browse files
committed
Add more details about the structures/unions in the documentation
1 parent 82fee20 commit cdb51ac

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

doc/user_manual.md

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ The user may use this enum with some other elements that will be discussed in th
1919
`File: cjlib.h`
2020

2121
## JSON structures
22+
23+
## cjlib_json
2224
To group all the information required for the JSON to be represented in the memory, cjlib has implemented a structure called **cjlib_json** which is defined as follows
2325

2426
```C
@@ -30,14 +32,59 @@ struct cjlib_json
3032
};
3133
```
3234

33-
`The user should not modify the contents of this structure in the code, as it may break the internal operation of the library, leading to memory leaks and memory corruption. Only the library functions must have the right to modify the contents of this structure. Where it is required, the user may access, it for read purposes only (of course I would like to make the declaration of the structure private, but in this case, it is less flexible and requires more memory allocations and systemcalls, therefore is less efficient in this way).`
35+
`The user should not modify the contents of this structure in the code, as it may break the internal operation of the library, leading to memory leaks and memory corruption. Only the library functions must have the right to modify the contents of this structure. If it is required, the user may access it for read purposes only (of course I would like to make the declaration of the structure private, but in this case, it is less flexible and requires more memory allocations and systemcalls, therefore is less efficient in this way).`
36+
37+
`File: cjlib.h`
38+
39+
## cjlib_json_data
40+
The most important structure for the library user is the **cjlib_json_data** structure. This structure consists of the information needed to save the required data to the internal data structure of the cjlib. The structure is defined as shown below:
41+
42+
```C
43+
struct cjlib_json_data
44+
{
45+
union cjlib_json_data_disting c_value; // The data to store/retrieve.
46+
enum cjlib_json_datatypes c_datatype; // The type of the data.
47+
};
48+
```
49+
50+
This structure consists of two elements. The first is the **c_value** which represents the JSON data to be saved/retrieved (boolean, number, object, arrays). The second represents the type of data stored in the **c_value** member. The **c_datatype** member will not be discussed further, as the **Json types** section described the available types that could be stored in this member. The member to which the focus will be pointed is the c_value member. As shown in the definition of the structure, this field/member is another built-in data structure of C, which is a union, that helps to distinguish between the available types of the JSON, using only one single and simple data structure. Shown below is the definition of the union that represents the data of the JSON.
51+
52+
```C
53+
union cjlib_json_data_disting
54+
{
55+
char *c_str; // string
56+
cjlib_json_num c_num; // Number
57+
cjlib_json_bool c_boolean; // Boolean.
58+
cjlib_json_object *c_obj; // json object.
59+
void *c_null; // null
60+
cjlib_json_array *c_arr; // array.
61+
};
62+
63+
```
64+
65+
The user may use both the cjlib_json_data structure and the cjlib_json_data_disting union to determine 1) the type of the data that was stored/retrieved from the internal representation of the JSON in the memory and 2) to receive/store the respective value from/in the corresponding field of the union. Following, there is an illustrative example that creates simple data, a number, using the definitions presented here.
66+
67+
```C
68+
#include "cjlib.h"
69+
70+
int main(void) {
71+
struct cjlib_json_data my_data; // Where the data are stored.
72+
73+
my_data.c_datatype = CJLIB_NUMBER; // The type of the data.
74+
my_data.c_value = 50; // The actual data.
75+
// store the data in the internal data structure...
76+
}
77+
```
78+
79+
Further details on how to store/retrieve the data to/from the Internal data structure are described in the following sections.
80+
3481
3582
`File: cjlib.h`
3683
3784
# Errors
3885
3986
## Error types
40-
The library provides the user with a plethora of errors for the correct error handling. To do this, it provides the following types of errors that may appear when manipulating JSON files.
87+
The library provides the user with a plethora of errors for error handling. To do this, it provides the following types of errors that may appear when manipulating JSON files.
4188
4289
``` C
4390
enum cjlib_json_error_types

0 commit comments

Comments
 (0)