json: Add top-level array encoding support

The library supports the declaration of JSON arrays as both nested and
top-level elements. However, as the provided encoding functions
json_obj_encode() and json_obj_encode_buf() interpret all input
structures as objects, top-level arrays are encoded as

{"<field_name>":[{...},...,{...}]}

instead of

[{...},...,{...}].

Add new functions json_arr_encode() and json_arr_encode_buf() that
enable top-level JSON array encoding.

Signed-off-by: Markus Fuchs <markus.fuchs@de.sauter-bc.com>
This commit is contained in:
Markus Fuchs 2020-06-05 12:46:02 +02:00 committed by Carles Cufí
commit 2f9b0d419b
2 changed files with 53 additions and 0 deletions

View file

@ -654,6 +654,24 @@ ssize_t json_calc_encoded_len(const struct json_obj_descr *descr,
int json_obj_encode_buf(const struct json_obj_descr *descr, size_t descr_len,
const void *val, char *buffer, size_t buf_size);
/**
* @brief Encodes an array in a contiguous memory location
*
* @param descr Pointer to the descriptor array
*
* @param val Struct holding the values
*
* @param buffer Buffer to store the JSON data
*
* @param buf_size Size of buffer, in bytes, with space for the terminating
* NUL character
*
* @return 0 if object has been successfully encoded. A negative value
* indicates an error (as defined on errno.h).
*/
int json_arr_encode_buf(const struct json_obj_descr *descr, const void *val,
char *buffer, size_t buf_size);
/**
* @brief Encodes an object using an arbitrary writer function
*
@ -675,6 +693,24 @@ int json_obj_encode(const struct json_obj_descr *descr, size_t descr_len,
const void *val, json_append_bytes_t append_bytes,
void *data);
/**
* @brief Encodes an array using an arbitrary writer function
*
* @param descr Pointer to the descriptor array
*
* @param val Struct holding the values
*
* @param append_bytes Function to append bytes to the output
*
* @param data Data pointer to be passed to the append_bytes callback
* function.
*
* @return 0 if object has been successfully encoded. A negative value
* indicates an error.
*/
int json_arr_encode(const struct json_obj_descr *descr, const void *val,
json_append_bytes_t append_bytes, void *data);
#ifdef __cplusplus
}
#endif