|
1 | 1 | # JSON |
2 | 2 | ## JSON types |
| 3 | +The JSON standard features a plethora of types, like **strings**, **booleans**, **numbers** (both floating points and integers), and **null**. Moreover, it features some more complex data structures such as **arrays** and **objects**. To accommodate this, cjlib has implemented the following enum, which is used to differentiate between the forenamed types and data structures. |
| 4 | + |
| 5 | +```C |
| 6 | +enum cjlib_json_datatypes |
| 7 | +{ |
| 8 | + CJLIB_STRING, |
| 9 | + CJLIB_NUMBER, |
| 10 | + CJLIB_ARRAY, |
| 11 | + CJLIB_BOOLEAN, |
| 12 | + CJLIB_OBJECT, |
| 13 | + CJLIB_NULL |
| 14 | +}; |
| 15 | +``` |
| 16 | + |
| 17 | +The user may use this enum with some other elements that will be discussed in the following sections, to perform various manipulations on the JSON contents. |
| 18 | + |
| 19 | +`File: cjlib.h` |
3 | 20 |
|
4 | 21 | ## JSON structures |
| 22 | +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 |
5 | 23 |
|
6 | | -# Errors |
| 24 | +```C |
| 25 | +struct cjlib_json |
| 26 | +{ |
| 27 | + cjlib_json_fd c_fp; // The file pointer of the json file. |
| 28 | + cjlib_json_object *c_dict; // The dictionary that contains the values of the json. |
| 29 | + char *c_path; // The path to the respective file. |
| 30 | +}; |
| 31 | +``` |
| 32 | + |
| 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).` |
| 34 | + |
| 35 | +`File: cjlib.h` |
| 36 | + |
| 37 | +# Errors |
7 | 38 |
|
8 | 39 | ## 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. |
| 41 | + |
| 42 | +``` C |
| 43 | +enum cjlib_json_error_types |
| 44 | +{ |
| 45 | + NO_ERROR, |
| 46 | + UNDEFINED, |
| 47 | + INVALID_TYPE, |
| 48 | + INVALID_JSON, |
| 49 | + DUPLICATE_NAME, |
| 50 | + INVALID_PROPERTY, |
| 51 | + MISSING_SEPERATOR, |
| 52 | + MEMORY_ERROR, |
| 53 | + INCOMPLETE_CURLY_BRACKETS, |
| 54 | + INCOMPLETE_SQUARE_BRACKETS, |
| 55 | + INCOMPLETE_DOUBLE_QUOTES, |
| 56 | + MISSING_COMMA, |
| 57 | + INVALID_NUMBER |
| 58 | +}; |
| 59 | + |
| 60 | +``` |
| 61 | + |
| 62 | +`File: cjlib_error.h` |
9 | 63 |
|
10 | 64 | ## Error structure |
| 65 | +To help discover the cause of an error and therefore handle it with the appropriate actions, cjlib provides the following structure with as much information as possible for the error that is caused. In some cases, some fields of this structure may be left empty, because the internal function builds the errors while parsing or editing the JSON file. Depending on the state where the error occurred, more or less information may be available about the error. |
11 | 66 |
|
| 67 | +```C |
| 68 | +struct cjlib_json_error |
| 69 | +{ |
| 70 | + char *c_property_name; // The name of the property in which the error occurred. |
| 71 | + char *c_property_value; // The value of the property in which the error occurred. |
| 72 | + enum cjlib_json_error_types c_error_code; // A error code indicating the error. |
| 73 | +}; |
| 74 | + |
| 75 | +``` |
| 76 | +`File: cjlib_error.h` |
12 | 77 |
|
13 | 78 | # Error related functions |
| 79 | +Cjlib provides the errors by manipulating an internal (private) variable in the implementation of cjlib_error.c file. This is done to protect it from mistaken actions. To access the private variable that has the information about the error that occurred, the library provides the following functions. Each of the following functions is **thread safe**, as it uses mutex, to allow multiple threads to check for errors (if this is necessary). |
14 | 80 |
|
15 | 81 | ___ |
16 | 82 | ## cjlib_json_error_init |
|
0 commit comments