Debian Codegraph @

crt/aws-c-common/include/aws/common/json.h

raw symbols SCIP
1 #ifndef AWS_COMMON_JSON_H
2 #define AWS_COMMON_JSON_H
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8
9 #include <aws/common/byte_buf.h>
10 #include <aws/common/common.h>
11
12 AWS_PUSH_SANE_WARNING_LEVEL
13
14 struct aws_json_value;
15
16 AWS_EXTERN_C_BEGIN
17
18 // ====================
19 // Create and pass type
20
21 /**
22 * Creates a new string aws_json_value with the given string and returns a pointer to it.
23 *
24 * Note: You will need to free the memory for the aws_json_value using aws_json_destroy on the aws_json_value or
25 * on the object/array containing the aws_json_value.
26 * Note: might be slower than c_str version due to internal copy
27 * @param string A byte cursor you want to store in the aws_json_value
28 * @param allocator The allocator to use when creating the value
29 * @return A new string aws_json_value
30 */
31 AWS_COMMON_API
32 struct aws_json_value *aws_json_value_new_string(struct aws_allocator *allocator, struct aws_byte_cursor string);
33
34 /**
35 * Creates a new string aws_json_value with the given string and returns a pointer to it.
36 *
37 * Note: You will need to free the memory for the aws_json_value using aws_json_destroy on the aws_json_value or
38 * on the object/array containing the aws_json_value.
39 * @param string c string pointer you want to store in the aws_json_value
40 * @param allocator The allocator to use when creating the value
41 * @return A new string aws_json_value
42 */
43 AWS_COMMON_API
44 struct aws_json_value *aws_json_value_new_string_from_c_str(struct aws_allocator *allocator, const char *string);
45
46 /**
47 * Creates a new number aws_json_value with the given number and returns a pointer to it.
48 *
49 * Note: You will need to free the memory for the aws_json_value using aws_json_destroy on the aws_json_value or
50 * on the object/array containing the aws_json_value.
51 * @param number The number you want to store in the aws_json_value
52 * @param allocator The allocator to use when creating the value
53 * @return A new number aws_json_value
54 */
55 AWS_COMMON_API
56 struct aws_json_value *aws_json_value_new_number(struct aws_allocator *allocator, double number);
57
58 /**
59 * Creates a new array aws_json_value and returns a pointer to it.
60 *
61 * Note: You will need to free the memory for the aws_json_value using aws_json_destroy on the aws_json_value or
62 * on the object/array containing the aws_json_value.
63 * Deleting this array will also destroy any aws_json_values it contains.
64 * @param allocator The allocator to use when creating the value
65 * @return A new array aws_json_value
66 */
67 AWS_COMMON_API
68 struct aws_json_value *aws_json_value_new_array(struct aws_allocator *allocator);
69
70 /**
71 * Creates a new boolean aws_json_value with the given boolean and returns a pointer to it.
72 *
73 * Note: You will need to free the memory for the aws_json_value using aws_json_destroy on the aws_json_value or
74 * on the object/array containing the aws_json_value.
75 * @param boolean The boolean you want to store in the aws_json_value
76 * @param allocator The allocator to use when creating the value
77 * @return A new boolean aws_json_value
78 */
79 AWS_COMMON_API
80 struct aws_json_value *aws_json_value_new_boolean(struct aws_allocator *allocator, bool boolean);
81
82 /**
83 * Creates a new null aws_json_value and returns a pointer to it.
84 *
85 * Note: You will need to free the memory for the aws_json_value using aws_json_destroy on the aws_json_value or
86 * on the object/array containing the aws_json_value.
87 * @param allocator The allocator to use when creating the value
88 * @return A new null aws_json_value
89 */
90 AWS_COMMON_API
91 struct aws_json_value *aws_json_value_new_null(struct aws_allocator *allocator);
92
93 /**
94 * Creates a new object aws_json_value and returns a pointer to it.
95 *
96 * Note: You will need to free the memory for the aws_json_value using aws_json_destroy on the aws_json_value or
97 * on the object/array containing the aws_json_value.
98 * Deleting this object will also destroy any aws_json_values it contains.
99 * @param allocator The allocator to use when creating the value
100 * @return A new object aws_json_value
101 */
102 AWS_COMMON_API
103 struct aws_json_value *aws_json_value_new_object(struct aws_allocator *allocator);
104 // ====================
105
106 // ====================
107 // Value getters
108
109 /**
110 * Gets the string of a string aws_json_value.
111 * @param value The string aws_json_value.
112 * @param output The string
113 * @return AWS_OP_SUCCESS if the value is a string, otherwise AWS_OP_ERR.
114 */
115 AWS_COMMON_API
116 int aws_json_value_get_string(const struct aws_json_value *value, struct aws_byte_cursor *output);
117
118 /**
119 * Gets the number of a number aws_json_value.
120 * @param value The number aws_json_value.
121 * @param output The number
122 * @return AWS_OP_SUCCESS if the value is a number, otherwise AWS_OP_ERR.
123 */
124 AWS_COMMON_API
125 int aws_json_value_get_number(const struct aws_json_value *value, double *output);
126
127 /**
128 * Gets the boolean of a boolean aws_json_value.
129 * @param value The boolean aws_json_value.
130 * @param output The boolean
131 * @return AWS_OP_SUCCESS if the value is a boolean, otherwise AWS_OP_ERR.
132 */
133 AWS_COMMON_API
134 int aws_json_value_get_boolean(const struct aws_json_value *value, bool *output);
135 // ====================
136
137 // ====================
138 // Object API
139
140 /**
141 * Adds a aws_json_value to a object aws_json_value.
142 *
143 * Note that the aws_json_value will be destroyed when the aws_json_value object is destroyed
144 * by calling "aws_json_destroy()"
145 * Note: might be slower than c_str version due to internal copy
146 * @param object The object aws_json_value you want to add a value to.
147 * @param key The key to add the aws_json_value at.
148 * @param value The aws_json_value you want to add.
149 * @return AWS_OP_SUCCESS if adding was successful.
150 * Will return AWS_OP_ERROR if the object passed is invalid or if the passed key
151 * is already in use in the object.
152 */
153 AWS_COMMON_API
154 int aws_json_value_add_to_object(
155 struct aws_json_value *object,
156 struct aws_byte_cursor key,
157 struct aws_json_value *value);
158
159 /**
160 * Adds a aws_json_value to a object aws_json_value.
161 *
162 * Note that the aws_json_value will be destroyed when the aws_json_value object is destroyed
163 * by calling "aws_json_destroy()"
164 * @param object The object aws_json_value you want to add a value to.
165 * @param key The key to add the aws_json_value at.
166 * @param value The aws_json_value you want to add.
167 * @return AWS_OP_SUCCESS if adding was successful.
168 * Will return AWS_OP_ERROR if the object passed is invalid or if the passed key
169 * is already in use in the object.
170 */
171 AWS_COMMON_API
172 int aws_json_value_add_to_object_c_str(struct aws_json_value *object, const char *key, struct aws_json_value *value);
173
174 /**
175 * Returns the aws_json_value at the given key.
176 * Note: might be slower than c_str version due to internal copy
177 * @param object The object aws_json_value you want to get the value from.
178 * @param key The key that the aws_json_value is at. Is case sensitive.
179 * @return The aws_json_value at the given key, otherwise NULL.
180 */
181 AWS_COMMON_API
182 struct aws_json_value *aws_json_value_get_from_object(const struct aws_json_value *object, struct aws_byte_cursor key);
183
184 /**
185 * Returns the aws_json_value at the given key.
186 * Note: same as aws_json_value_get_from_object but with key as const char *.
187 * Prefer this method is you have a key thats already a valid char * as it is likely to be faster.
188 * @param object The object aws_json_value you want to get the value from.
189 * @param key The key that the aws_json_value is at. Is case sensitive.
190 * @return The aws_json_value at the given key, otherwise NULL.
191 */
192 AWS_COMMON_API
193 struct aws_json_value *aws_json_value_get_from_object_c_str(const struct aws_json_value *object, const char *key);
194
195 /**
196 * Checks if there is a aws_json_value at the given key.
197 * Note: might be slower than c_str version due to internal copy
198 * @param object The value aws_json_value you want to check a key in.
199 * @param key The key that you want to check. Is case sensitive.
200 * @return True if a aws_json_value is found.
201 */
202 AWS_COMMON_API
203 bool aws_json_value_has_key(const struct aws_json_value *object, struct aws_byte_cursor key);
204
205 /**
206 * Checks if there is a aws_json_value at the given key.
207 * Note: same as aws_json_value_has_key but with key as const char *.
208 * Prefer this method is you have a key thats already a valid char * as it is likely to be faster.
209 * @param object The value aws_json_value you want to check a key in.
210 * @param key The key that you want to check. Is case sensitive.
211 * @return True if a aws_json_value is found.
212 */
213 AWS_COMMON_API
214 bool aws_json_value_has_key_c_str(const struct aws_json_value *object, const char *key);
215
216 /**
217 * Removes the aws_json_value at the given key.
218 * Note: might be slower than c_str version due to internal copy
219 * @param object The object aws_json_value you want to remove a aws_json_value in.
220 * @param key The key that the aws_json_value is at. Is case sensitive.
221 * @return AWS_OP_SUCCESS if the aws_json_value was removed.
222 * Will return AWS_OP_ERR if the object passed is invalid or if the value
223 * at the key cannot be found.
224 */
225 AWS_COMMON_API
226 int aws_json_value_remove_from_object(struct aws_json_value *object, struct aws_byte_cursor key);
227
228 /**
229 * Removes the aws_json_value at the given key.
230 * Note: same as aws_json_value_remove_from_object but with key as const char *.
231 * Prefer this method is you have a key thats already a valid char * as it is likely to be faster.
232 * @param object The object aws_json_value you want to remove a aws_json_value in.
233 * @param key The key that the aws_json_value is at. Is case sensitive.
234 * @return AWS_OP_SUCCESS if the aws_json_value was removed.
235 * Will return AWS_OP_ERR if the object passed is invalid or if the value
236 * at the key cannot be found.
237 */
238 AWS_COMMON_API
239 int aws_json_value_remove_from_object_c_str(struct aws_json_value *object, const char *key);
240
241 /**
242 * @brief callback for iterating members of an object
243 * Iteration can be controlled as follows:
244 * - return AWS_OP_SUCCESS and out_should_continue is set to true (default value) -
245 * continue iteration without error
246 * - return AWS_OP_SUCCESS and out_continue is set to false -
247 * stop iteration without error
248 * - return AWS_OP_ERR - stop iteration with error
249 */
250 typedef int(aws_json_on_member_encountered_const_fn)(
251 const struct aws_byte_cursor *key,
252 const struct aws_json_value *value,
253 bool *out_should_continue,
254 void *user_data);
255
256 /**
257 * @brief iterates through members of the object.
258 * iteration is sequential in order fields were initially parsed.
259 * @param object object to iterate over.
260 * @param on_member callback for when member is encountered.
261 * @param user_data user data to pass back in callback.
262 * @return AWS_OP_SUCCESS when iteration finishes completely or exits early,
263 * AWS_OP_ERR if value is not an object.
264 */
265 AWS_COMMON_API
266 int aws_json_const_iterate_object(
267 const struct aws_json_value *object,
268 aws_json_on_member_encountered_const_fn *on_member,
269 void *user_data);
270
271 // ====================
272
273 // ====================
274 // Array API
275
276 /**
277 * Adds a aws_json_value to the given array aws_json_value.
278 *
279 * Note that the aws_json_value will be destroyed when the aws_json_value array is destroyed
280 * by calling "aws_json_destroy()"
281 * @param array The array aws_json_value you want to add an aws_json_value to.
282 * @param value The aws_json_value you want to add.
283 * @return AWS_OP_SUCCESS if adding the aws_json_value was successful.
284 * Will return AWS_OP_ERR if the array passed is invalid.
285 */
286 AWS_COMMON_API
287 int aws_json_value_add_array_element(struct aws_json_value *array, const struct aws_json_value *value);
288
289 /**
290 * Returns the aws_json_value at the given index in the array aws_json_value.
291 * @param array The array aws_json_value.
292 * @param index The index of the aws_json_value you want to access.
293 * @return A pointer to the aws_json_value at the given index in the array, otherwise NULL.
294 */
295 AWS_COMMON_API
296 struct aws_json_value *aws_json_get_array_element(const struct aws_json_value *array, size_t index);
297
298 /**
299 * Returns the number of items in the array aws_json_value.
300 * @param array The array aws_json_value.
301 * @return The number of items in the array_json_value.
302 */
303 AWS_COMMON_API
304 size_t aws_json_get_array_size(const struct aws_json_value *array);
305
306 /**
307 * Removes the aws_json_value at the given index in the array aws_json_value.
308 * @param array The array aws_json_value.
309 * @param index The index containing the aws_json_value you want to remove.
310 * @return AWS_OP_SUCCESS if the aws_json_value at the index was removed.
311 * Will return AWS_OP_ERR if the array passed is invalid or if the index
312 * passed is out of range.
313 */
314 AWS_COMMON_API
315 int aws_json_value_remove_array_element(struct aws_json_value *array, size_t index);
316
317 /**
318 * @brief callback for iterating values of an array.
319 * Iteration can be controlled as follows:
320 * - return AWS_OP_SUCCESS and out_should_continue is set to true (default value) -
321 * continue iteration without error
322 * - return AWS_OP_SUCCESS and out_continue is set to false -
323 * stop iteration without error
324 * - return AWS_OP_ERR - stop iteration with error
325 */
326 typedef int(aws_json_on_value_encountered_const_fn)(
327 size_t index,
328 const struct aws_json_value *value,
329 bool *out_should_continue,
330 void *user_data);
331
332 /**
333 * @brief iterates through values of an array.
334 * iteration is sequential starting with 0th element.
335 * @param array array to iterate over.
336 * @param on_value callback for when value is encountered.
337 * @param user_data user data to pass back in callback.
338 * @return AWS_OP_SUCCESS when iteration finishes completely or exits early,
339 * AWS_OP_ERR if value is not an array.
340 */
341 AWS_COMMON_API
342 int aws_json_const_iterate_array(
343 const struct aws_json_value *array,
344 aws_json_on_value_encountered_const_fn *on_value,
345 void *user_data);
346
347 // ====================
348
349 // ====================
350 // Checks
351
352 /**
353 * Checks whether two json values are equivalent.
354 * @param a first value to compare.
355 * @param b second value to compare.
356 * @param is_case_sensitive case sensitive compare or not.
357 * @return True is values are equal, false otherwise
358 */
359 AWS_COMMON_API
360 bool aws_json_value_compare(const struct aws_json_value *a, const struct aws_json_value *b, bool is_case_sensitive);
361
362 /**
363 * Duplicates json value.
364 * @param value first value to compare.
365 * @return duplicated value. NULL and last error set if value cannot be duplicated.
366 */
367 AWS_COMMON_API
368 struct aws_json_value *aws_json_value_duplicate(const struct aws_json_value *value);
369
370 /**
371 * Checks if the aws_json_value is a string.
372 * @param value The aws_json_value to check.
373 * @return True if the aws_json_value is a string aws_json_value, otherwise false.
374 */
375 AWS_COMMON_API
376 bool aws_json_value_is_string(const struct aws_json_value *value);
377
378 /**
379 * Checks if the aws_json_value is a number.
380 * @param value The aws_json_value to check.
381 * @return True if the aws_json_value is a number aws_json_value, otherwise false.
382 */
383 AWS_COMMON_API
384 bool aws_json_value_is_number(const struct aws_json_value *value);
385
386 /**
387 * Checks if the aws_json_value is a array.
388 * @param value The aws_json_value to check.
389 * @return True if the aws_json_value is a array aws_json_value, otherwise false.
390 */
391 AWS_COMMON_API
392 bool aws_json_value_is_array(const struct aws_json_value *value);
393
394 /**
395 * Checks if the aws_json_value is a boolean.
396 * @param value The aws_json_value to check.
397 * @return True if the aws_json_value is a boolean aws_json_value, otherwise false.
398 */
399 AWS_COMMON_API
400 bool aws_json_value_is_boolean(const struct aws_json_value *value);
401
402 /**
403 * Checks if the aws_json_value is a null aws_json_value.
404 * @param value The aws_json_value to check.
405 * @return True if the aws_json_value is a null aws_json_value, otherwise false.
406 */
407 AWS_COMMON_API
408 bool aws_json_value_is_null(const struct aws_json_value *value);
409
410 /**
411 * Checks if the aws_json_value is a object aws_json_value.
412 * @param value The aws_json_value to check.
413 * @return True if the aws_json_value is a object aws_json_value, otherwise false.
414 */
415 AWS_COMMON_API
416 bool aws_json_value_is_object(const struct aws_json_value *value);
417 // ====================
418
419 // ====================
420 // Memory Management
421
422 /**
423 * Removes the aws_json_value from memory. If the aws_json_value is a object or array, it will also destroy
424 * attached aws_json_values as well.
425 *
426 * For example, if you called "aws_json_array_add(b, a)" to add an object "a" to an array "b", if you call
427 * "aws_json_destroy(b)" then it will also free "a" automatically. All children/attached aws_json_values are freed
428 * when the parent/root aws_json_value is destroyed.
429 * @param value The aws_json_value to destroy.
430 */
431 AWS_COMMON_API
432 void aws_json_value_destroy(struct aws_json_value *value);
433 // ====================
434
435 // ====================
436 // Utility
437
438 /**
439 * Appends a unformatted JSON string representation of the aws_json_value into the passed byte buffer.
440 * The byte buffer is expected to be already initialized so the function can append the JSON into it.
441 *
442 * Note: The byte buffer will automatically have its size extended if the JSON string is over the byte
443 * buffer capacity AND the byte buffer has an allocator associated with it. If the byte buffer does not
444 * have an allocator associated and the JSON string is over capacity, AWS_OP_ERR will be returned.
445 *
446 * Note: When you are finished with the aws_byte_buf, you must call "aws_byte_buf_clean_up_secure" to free
447 * the memory used, as it will NOT be called automatically.
448 * @param value The aws_json_value to format.
449 * @param output The destination for the JSON string
450 * @return AWS_OP_SUCCESS if the JSON string was allocated to output without any errors
451 * Will return AWS_OP_ERR if the value passed is not an aws_json_value or if there
452 * was an error appending the JSON into the byte buffer.
453 */
454 AWS_COMMON_API
455 int aws_byte_buf_append_json_string(const struct aws_json_value *value, struct aws_byte_buf *output);
456
457 /**
458 * Appends a formatted JSON string representation of the aws_json_value into the passed byte buffer.
459 * The byte buffer is expected to already be initialized so the function can append the JSON into it.
460 *
461 * Note: The byte buffer will automatically have its size extended if the JSON string is over the byte
462 * buffer capacity AND the byte buffer has an allocator associated with it. If the byte buffer does not
463 * have an allocator associated and the JSON string is over capacity, AWS_OP_ERR will be returned.
464 *
465 * Note: When you are finished with the aws_byte_buf, you must call "aws_byte_buf_clean_up_secure" to free
466 * the memory used, as it will NOT be called automatically.
467 * @param value The aws_json_value to format.
468 * @param output The destination for the JSON string
469 * @return AWS_OP_SUCCESS if the JSON string was allocated to output without any errors
470 * Will return AWS_OP_ERR if the value passed is not an aws_json_value or if there
471 * aws an error appending the JSON into the byte buffer.
472 */
473 AWS_COMMON_API
474 int aws_byte_buf_append_json_string_formatted(const struct aws_json_value *value, struct aws_byte_buf *output);
475
476 /**
477 * Parses the JSON string and returns a aws_json_value containing the root of the JSON.
478 * @param allocator The allocator used to create the value
479 * @param string The string containing the JSON.
480 * @return The root aws_json_value of the JSON.
481 */
482 AWS_COMMON_API
483 struct aws_json_value *aws_json_value_new_from_string(struct aws_allocator *allocator, struct aws_byte_cursor string);
484 // ====================
485
486 AWS_EXTERN_C_END
487 AWS_POP_SANE_WARNING_LEVEL
488
489 #endif // AWS_COMMON_JSON_H