{"thread":{"id":"63309","subject":"[GSoC PATCH] json-writer: add docstrings to jw_* functions","startedAt":"2025-04-18T16:57:01Z","lastAt":"2025-04-22T21:24:18Z","messageCount":3,"participants":["Lucas Seiki Oshiro","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"516370","messageId":"20250418165651.14125-1-lucasseikioshiro@gmail.com","threadId":"63309","inReplyTo":null,"subject":"[GSoC PATCH] json-writer: add docstrings to jw_* functions","fromName":"Lucas Seiki Oshiro","fromEmail":"lucasseikioshiro@gmail.com","sentAt":"2025-04-18T16:56:51Z","receivedAt":"2025-04-18T16:57:01Z","isPatch":true,"sender":{"key":"lucasseikioshiro@gmail.com","avatar":"https://avatars.githubusercontent.com/u/12701580?v=4"},"body":"Add a docstring for each function that manipulates json_writers.\n---\nHi!\n\nGiven that my GSoC project needs some form of formatting JSON output, the\nalready existent json-writer.{ch} will be extremely useful. So, before\nGSoC actually starts, I decided to study a little about json-writer and\njust found that it doesn't have docstrings.\n\nHere I'm documenting what each function does, as this module is a general\nutility that can be used in any place of the Git codebase and other people\nmay be in the same position of studying of it does by directly reading its\nsource code.\n\nPS: I'm sending this as single patch as many docstrings are similar and\njson_writer was introduced in a single patch (7545941). But I can break\nit into smaller patches if you prefer :-).\n\n json-writer.h | 133 ++++++++++++++++++++++++++++++++++++++++++++++++++\n 1 file changed, 133 insertions(+)\n\ndiff --git a/json-writer.h b/json-writer.h\nindex 04413bd1af..aa513e86cb 100644\n--- a/json-writer.h\n+++ b/json-writer.h\n@@ -69,42 +69,175 @@ struct json_writer\n \t.open_stack = STRBUF_INIT, \\\n }\n \n+/*\n+ * Initialize a json_writer with empty values.\n+ */\n void jw_init(struct json_writer *jw);\n+\n+/*\n+ * Release the internal buffers of a json_writer.\n+ */\n void jw_release(struct json_writer *jw);\n \n+/*\n+ * Begin the json_writer using an object as the top-level data structure. If\n+ * pretty is set to 1, the result will be a human-readable and indented JSON,\n+ * and if it is set to 0 the result will be minified single-line JSON.\n+ */\n void jw_object_begin(struct json_writer *jw, int pretty);\n+\n+/*\n+ * Begin the json_writer using an array as the top-level data structure. If\n+ * pretty is set to 1, the result will be a human-readable and indented JSON,\n+ * and if it is set to 0 the result will be minified single-line JSON.\n+ */\n void jw_array_begin(struct json_writer *jw, int pretty);\n \n+/*\n+ * Append a string field to the current object of the json_writer, given its key\n+ * and its value.\n+ */\n void jw_object_string(struct json_writer *jw, const char *key,\n \t\t      const char *value);\n+\n+/*\n+ * Append an int field to the current object of the json_writer, given its key\n+ * and its value.\n+ */\n void jw_object_intmax(struct json_writer *jw, const char *key, intmax_t value);\n+\n+/*\n+ * Append a double field to the current object of the json_writer, given its key\n+ * and its value. The precision parameter can be used for specifying the number\n+ * of decimals after the point, using -1 for formatting with the maximum\n+ * precision available.\n+ */\n void jw_object_double(struct json_writer *jw, const char *key, int precision,\n \t\t      double value);\n+\n+/*\n+ * Append a boolean field set to true to the current object of the json_writer,\n+ * given its key.\n+ */\n void jw_object_true(struct json_writer *jw, const char *key);\n+\n+/*\n+ * Append a boolean field set to false to the current object of the json_writer,\n+ * given its key.\n+ */\n void jw_object_false(struct json_writer *jw, const char *key);\n+\n+/*\n+ * Append a boolean field to the current object of the json_writer, given its\n+ * key and its value.\n+ */\n void jw_object_bool(struct json_writer *jw, const char *key, int value);\n+\n+/*\n+ * Append a null field to the current object of the json_writer, given its key.\n+ */\n void jw_object_null(struct json_writer *jw, const char *key);\n+\n+/*\n+ * Append a field to the current object of the json_writer, given its key and\n+ * another json_writer that represents its content.\n+ */\n void jw_object_sub_jw(struct json_writer *jw, const char *key,\n \t\t      const struct json_writer *value);\n \n+/*\n+ * Start an object as the value of a field in the current object of the\n+ * json_writer, given the field key.\n+ */\n void jw_object_inline_begin_object(struct json_writer *jw, const char *key);\n+\n+/*\n+ * Start an array as the value of a field in the current object of the\n+ * json_writer, given the field key.\n+ */\n void jw_object_inline_begin_array(struct json_writer *jw, const char *key);\n \n+/*\n+ * Append a string value to the current array of the json_writer.\n+ */\n void jw_array_string(struct json_writer *jw, const char *value);\n+\n+/*\n+ * Append an int value to the current array of the json_writer.\n+ */\n void jw_array_intmax(struct json_writer *jw, intmax_t value);\n+\n+/*\n+ * Append a double value to the current array of the json_writer. The precision\n+ * parameter can be used for specifying the number of decimals after the point,\n+ * using -1 for formatting with the maximum precision available.\n+ */\n void jw_array_double(struct json_writer *jw, int precision, double value);\n+\n+/*\n+ * Append a true value to the current array of the json_writer.\n+ */\n void jw_array_true(struct json_writer *jw);\n+\n+/*\n+ * Append a false value to the current array of the json_writer.\n+ */\n void jw_array_false(struct json_writer *jw);\n+\n+/*\n+ * Append a boolean value to the current array of the json_writer.\n+ */\n void jw_array_bool(struct json_writer *jw, int value);\n+\n+/*\n+ * Append a null value to the current array of the json_writer.\n+ */\n void jw_array_null(struct json_writer *jw);\n+\n+/*\n+ * Append a value to the current array of the json_writer, given the\n+ * json_writer that represents its content.\n+ */\n void jw_array_sub_jw(struct json_writer *jw, const struct json_writer *value);\n+\n+/*\n+ * Append the first argc values from the argv array of strings to the current\n+ * array of the json_writer.\n+ *\n+ * This function does not provide safety for cases where the array has less than\n+ * argc values.\n+ */\n void jw_array_argc_argv(struct json_writer *jw, int argc, const char **argv);\n+\n+/*\n+ * Append a null-terminated array of strings to the current array of the\n+ * json_writer.\n+ */\n void jw_array_argv(struct json_writer *jw, const char **argv);\n \n+/*\n+ * Start an object as a value in the current array of the json_writer.\n+ */\n void jw_array_inline_begin_object(struct json_writer *jw);\n+\n+/*\n+ * Start an array as a value in the current array.\n+ */\n void jw_array_inline_begin_array(struct json_writer *jw);\n \n+/*\n+ * Return if the json_writer is terminated. In other words, if the all the\n+ * objects and arrays are already closed.\n+ */\n int jw_is_terminated(const struct json_writer *jw);\n+\n+/*\n+ * Terminates the current object or array of the json_writer. In other words,\n+ * append a ] if the current array is not closed or } if the current object\n+ * is not closed.\n+ *\n+ * Abort the execution if there's no object or array that can be terminated.\n+ */\n void jw_end(struct json_writer *jw);\n \n #endif /* JSON_WRITER_H */\n-- \n2.39.5 (Apple Git-154)\n\n"},{"id":"516377","messageId":"xmqq7c3hb4cj.fsf@gitster.g","threadId":"63309","inReplyTo":"20250418165651.14125-1-lucasseikioshiro@gmail.com","subject":"Re: [GSoC PATCH] json-writer: add docstrings to jw_* functions","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2025-04-18T20:29:48Z","receivedAt":"2025-04-18T20:29:51Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Lucas Seiki Oshiro <lucasseikioshiro@gmail.com> writes:\n\n> Add a docstring for each function that manipulates json_writers.\n> ---\n> Hi!\n>\n> Given that my GSoC project needs some form of formatting JSON output, the\n> already existent json-writer.{ch} will be extremely useful. So, before\n> GSoC actually starts, I decided to study a little about json-writer and\n> just found that it doesn't have docstrings.\n>\n> Here I'm documenting what each function does, as this module is a general\n> utility that can be used in any place of the Git codebase and other people\n> may be in the same position of studying of it does by directly reading its\n> source code.\n>\n> PS: I'm sending this as single patch as many docstrings are similar and\n> json_writer was introduced in a single patch (7545941). But I can break\n> it into smaller patches if you prefer :-).\n\nNeeds sign-off.\n\n> +/*\n> + * Begin the json_writer using an object as the top-level data structure. If\n> + * pretty is set to 1, the result will be a human-readable and indented JSON,\n> + * and if it is set to 0 the result will be minified single-line JSON.\n> + */\n>  void jw_object_begin(struct json_writer *jw, int pretty);\n> +\n> +/*\n> + * Begin the json_writer using an array as the top-level data structure. If\n> + * pretty is set to 1, the result will be a human-readable and indented JSON,\n> + * and if it is set to 0 the result will be minified single-line JSON.\n> + */\n>  void jw_array_begin(struct json_writer *jw, int pretty);\n\nThese are not strictly wrong per-se in the sense that you can indeed\nwrite a \"top-level\" array by array-begin, followed by array-string,\netc., and finish it with end.  But such an array can be embedded as\na sub data structure in another json-writer with array-sub-jw or\nobject-sub-jw and once it is done, it is not \"top-level\" at all.\n\nPerhaps it may be beneficial to give an overview of the API design,\nat the beginning of the file (in other words, not a per-function\ncomment, but a comment covers the whole json-writer API), to outline\nthe concepts and philosophy the json-writer takes to build json\nobjects, perhaps?  Covering (I am not trying to be exhaustive here,\nbut merely giving ideas):\n\n - json_writer is to build a \"collection\", which is either an object\n   or an array.  An object is a set of key-value pair where keys are\n   always strings and values can be of various types (including\n   objects and arrays).  An array is an ordered set of values, which\n   can be of various types (including objects and arrays).\n\n - you open an object with object-begin, define one key-value pair\n   at a time using various jw_object_<type> functions, and conclude\n   with jw-end.\n\n - you open an array with array-begin, append one value at a  tie\n   using various jw_array_<type> functions, and conclude with\n   jw-end.\n\nor something along that line, perhaps?\n"},{"id":"516533","messageId":"EE721541-8445-40EF-9AAD-ED88B3961546@gmail.com","threadId":"63309","inReplyTo":"xmqq7c3hb4cj.fsf@gitster.g","subject":"Re: [GSoC PATCH] json-writer: add docstrings to jw_* functions","fromName":"Lucas Seiki Oshiro","fromEmail":"lucasseikioshiro@gmail.com","sentAt":"2025-04-22T21:24:03Z","receivedAt":"2025-04-22T21:24:18Z","isPatch":true,"sender":{"key":"lucasseikioshiro@gmail.com","avatar":"https://avatars.githubusercontent.com/u/12701580?v=4"},"body":"\n> But such an array can be embedded as a sub data structure in another\n> json-writer with array-sub-jw or object-sub-jw and once it is done,\n> it is not \"top-level\" at all.\n\nHmmmmm... Perhaps it would be better to replace it by something like\n\"the main data structure\" instead of \"top-level structure\"?\n\n> Perhaps it may be beneficial to give an overview of the API design,\n> at the beginning of the file (in other words, not a per-function\n> comment, but a comment covers the whole json-writer API), to outline\n> the concepts and philosophy the json-writer takes to build json\n> objects, perhaps?\n\nThe beginning of json_writer.h already provides a good overview of\nwhat it does, but not exactly to the functions. It also provides a\nreference to its associated test (which can be used as examples), but\nyeah, it is not exactly an API overview.\n\n> - json_writer is to build a \"collection\", which is either an object\n>   or an array.  An object is a set of key-value pair where keys are\n>   always strings and values can be of various types (including\n>   objects and arrays).  An array is an ordered set of values, which\n>   can be of various types (including objects and arrays).\n\nI think the already existing description covers those higher-level\naspects well enough.\n\n> or something along that line, perhaps?\n\nI liked it, and I'm working on it. But still, wouldn't it be nice\nto have descriptions on each function? An overview like that is\nenough for me for understanding most functions, but some are not so\nclear (e.g. jw_array_argc_argv and jw_array_argv). Or, to not\nbeing too verbose and repetitive only focusing in the less obvious\nones?\n\nThanks again, Junio!\n\nPS: I'm cc'ing the e-mail address of Jeff Hostetler provided in\nthe latest commit created by him, since the first message couldn't\nbe delivered. He was the author of json_writer and I sent the patch\ncc'ing the e-mail from the commit that introduced it.\n\n\n\n"}]}