Thu, 02 Jan 2025 19:07:56 +0100
add functions to create default JSON writers - relates to #526
src/cx/json.h | file | annotate | diff | comparison | revisions | |
src/json.c | file | annotate | diff | comparison | revisions |
--- a/src/cx/json.h Wed Jan 01 16:06:32 2025 +0100 +++ b/src/cx/json.h Thu Jan 02 19:07:56 2025 +0100 @@ -435,12 +435,37 @@ * The JSON writer settings. */ struct cx_json_writer_s { + /** + * Set true to enable pretty output. + */ bool pretty; + /** + * Set false to output the members in the order in which they were added. + */ bool sort_members; + /** + * The maximum number of fractional digits in a number value. + */ uint8_t frac_max_digits; + /** + * Set true to use spaces instead of tab characters. + * Indentation is only used in pretty output. + */ bool indent_space; + /** + * If \c indent_space is true, this is the number of spaces per tab. + * Indentation is only used in pretty output. + */ uint8_t indent; + /** + * Set true to enable automatic wrapping of arrays. + * Wrapping is only used in pretty output. + * Objects within arrays are always wrapped. + */ bool wrap_array; + /** + * Specify the maximum number of characters in a line before an array needs to wrap. + */ uint16_t wrap_threshold; }; @@ -449,6 +474,22 @@ */ typedef struct cx_json_writer_s CxJsonWriter; +/** + * Creates a default writer configuration for compact output. + * + * @return new JSON writer settings + */ +cx_attr_nodiscard +CxJsonWriter cxJsonWriterCompact(void); + +/** + * Creates a default writer configuration for pretty output. + * + * @param use_spaces false if you want tabs, true if you want four spaces instead + * @return new JSON writer settings + */ +cx_attr_nodiscard +CxJsonWriter cxJsonWriterPretty(bool use_spaces); /** * Writes a JSON value to a buffer or stream.
--- a/src/json.c Wed Jan 01 16:06:32 2025 +0100 +++ b/src/json.c Thu Jan 02 19:07:56 2025 +0100 @@ -923,12 +923,26 @@ true, 255, false, - 0, + 4, false, - 0 + 80 }; -// TODO: add default for pretty printing and add functions to create default structs +CxJsonWriter cxJsonWriterCompact(void) { + return cx_json_writer_default; +} + +CxJsonWriter cxJsonWriterPretty(bool use_spaces) { + return (CxJsonWriter) { + true, + true, + 255, + use_spaces, + 4, + false, + 80 + }; +} int cx_json_write_rec(