--- a/src/cx/json.h Wed Jan 01 15:26:50 2025 +0100 +++ b/src/cx/json.h Wed Jan 01 15:33:41 2025 +0100 @@ -432,6 +432,52 @@ typedef enum cx_json_status CxJsonStatus; /** + * The JSON writer settings. + */ +struct cx_json_writer_s { + bool pretty; + bool sort_members; + uint8_t frac_max_digits; + bool indent_space; + uint8_t indent; + bool wrap_array; + uint16_t wrap_threshold; +}; + +/** + * Typedef for the json writer. + */ +typedef struct cx_json_writer_s CxJsonWriter; + + +/** + * Writes a JSON value to a buffer or stream. + * + * This function blocks until all data is written or an error when trying + * to write data occurs. + * The write operation is not atomic in the sense that it might happen + * that the data is only partially written when an error occurs with no + * way to indicate how much data was written. + * To avoid this problem, you can use a CxBuffer as \p target which is + * unlikely to fail a write operation and either use the buffer's flush + * feature to relay the data or use the data in the buffer manually to + * write it to the actual target. + * + * @param target the buffer or stream where to write to + * @param value the value that shall be written + * @param wfunc the write function to use + * @param settings formatting settings (or \c NULL to use a compact default) + * @return zero on success, non-zero when no or not all data could be written + */ +cx_attr_nonnull_arg(1, 2, 3) +int cxJsonWrite( + void* target, + const CxJsonValue* value, + cx_write_func wfunc, + const CxJsonWriter* settings +); + +/** * Initializes the json interface. * * @param json the json interface @@ -1150,6 +1196,22 @@ CxIterator cxJsonArrIter(const CxJsonValue *value); /** + * Returns an iterator over the JSON object members. + * + * The iterator yields values of type \c CxJsonObjValue* which + * contain the name and value of the member. + * + * If the \p value is not a JSON object, the behavior is undefined. + * + * @param value the JSON value + * @return an iterator over the object members + * @see cxJsonIsObject() + */ +cx_attr_nonnull +cx_attr_nodiscard +CxIterator cxJsonObjIter(const CxJsonValue *value); + +/** * @copydoc cxJsonObjGet() */ cx_attr_nonnull