-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsn.h
181 lines (146 loc) · 4.53 KB
/
jsn.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#ifndef JSN_H
#define JSN_H
/**
* Author: Vernon Grant
* Repository: https://github.com/VernonGrant/jsn.c
* License: https://www.gnu.org/licenses/gpl-3.0.en.html
*
* Created: 2022-10-21
**/
#include <stdbool.h>
/* HANDLE DEFINITION.
* ------------------------------------------------------------------------- */
/**
* A handle is just a pointer to a node's struct. The tree structure is made out
* of many nodes.
*/
typedef struct jsn_node *jsn_handle;
/* PARSING, SAVING AND OUTPUTTING FUNCTIONS
* ------------------------------------------------------------------------- */
/**
* Prints the JSON of the provided handle (node).
*/
void jsn_print(jsn_handle handle);
/**
* Opens the given JSON file and parses it into a tree structure. It will
* call exit if there's any issues opening or parsing the file. Else it will
* return a handle to the root node.
*/
jsn_handle jsn_from_file(const char *file_path);
/**
* Will write the JSON of the given handle (node) to a file specified by the
* given path.
*/
void jsn_to_file(jsn_handle handle, const char *file_path);
/* TREE CREATION AND DELETION FUNCTIONS
* ------------------------------------------------------------------------- */
/**
* Creates an empty object node and return's it's handle.
*/
jsn_handle jsn_create_object();
/**
* Creates an empty array node and return's it's handle.
*/
jsn_handle jsn_create_array();
/**
* Creates an integer node and return's it's handle.
*/
jsn_handle jsn_create_integer(int value);
/**
* Creates an double node and return's it's handle.
*/
jsn_handle jsn_create_double(double value);
/**
* Creates an string node and return's it's handle.
*/
jsn_handle jsn_create_string(const char *value);
/**
* Creates an boolean node and return's it's handle.
*/
jsn_handle jsn_create_boolean(bool value);
/**
* Creates an null node and return's it's handle.
*/
jsn_handle jsn_create_null();
/**
* Will append a node onto the provided object (handle) and associates it with
* the given key. The first argument (handle) must be of an object type. It
* will return the provided node (value) handle.
*/
jsn_handle jsn_object_set(jsn_handle handle, const char *key, jsn_handle node);
/**
* Will append a node onto the end of an array. The first argument (handle)
* must be of an array type. It will return the provided node (value) handle.
*/
jsn_handle jsn_array_push(jsn_handle handle, jsn_handle node);
/**
* Returns the total number of children of the given handle.
*/
unsigned int jsn_array_count(jsn_handle handle);
/**
* Will recursively free the handle (node). Please note, that you should only
* every free the root node.
*/
void jsn_free(jsn_handle handle);
/* GETTING AND SETTING FUNCTIONS
* ------------------------------------------------------------------------- */
/**
* Returns a handle to an objects child node that matching the provided key
* hierarchy.
*/
jsn_handle jsn_get(jsn_handle handle, unsigned int arg_count, ...);
/**
* Returns a handle to an array child node, at the given index.
*/
jsn_handle jsn_get_array_item(jsn_handle handle, unsigned int index);
/**
* Get a nodes integer value.
*/
int jsn_get_value_int(jsn_handle handle);
/**
* Get a nodes boolean value.
*/
bool jsn_get_value_bool(jsn_handle handle);
/**
* Get a nodes double value.
*/
double jsn_get_value_double(jsn_handle handle);
/**
* Get a nodes string value.
*/
const char *jsn_get_value_string(jsn_handle handle);
/**
* Will return true if the handle (node) has a null value/type.
*/
bool jsn_is_value_null(jsn_handle handle);
/**
* Set's the given handle (node) as an object. Will mutate it's type if the
* handle is not of an object type.
*/
void jsn_set_as_object(jsn_handle handle);
/**
* Set's the given handle (node) as an array. Will mutate it's type if the
* handle is not of an array type.
*/
void jsn_set_as_array(jsn_handle handle);
/**
* Set's the given handle (node) as an integer. Will mutate it's type if the
* handle is not of an integer type.
*/
void jsn_set_as_integer(jsn_handle handle, int value);
/**
* Set's the given handle (node) as an double. Will mutate it's type if the
* handle is not of an double type.
*/
void jsn_set_as_double(jsn_handle handle, double value);
/**
* Set's the given handle (node) as an boolean. Will mutate it's type if the
* handle is not of an boolean type.
*/
void jsn_set_as_boolean(jsn_handle handle, bool value);
/**
* Set's the given handle (node) as an string. Will mutate it's type if the
* handle is not of an string type.
*/
void jsn_set_as_string(jsn_handle handle, const char *value);
#endif