1#ifndef JSON_WRITER_H 2#define JSON_WRITER_H 3 4/* 5 * JSON data structures are defined at: 6 * [1] http://www.ietf.org/rfc/rfc7159.txt 7 * [2] http://json.org/ 8 * 9 * The JSON-writer API allows one to build JSON data structures using a 10 * simple wrapper around a "struct strbuf" buffer. It is intended as a 11 * simple API to build output strings; it is not intended to be a general 12 * object model for JSON data. In particular, it does not re-order keys 13 * in an object (dictionary), it does not de-dup keys in an object, and 14 * it does not allow lookup or parsing of JSON data. 15 * 16 * All string values (both keys and string r-values) are properly quoted 17 * and escaped if they contain special characters. 18 * 19 * These routines create compact JSON data (with no unnecessary whitespace, 20 * newlines, or indenting). If you get an unexpected response, verify 21 * that you're not expecting a pretty JSON string. 22 * 23 * Both "JSON objects" (aka sets of k/v pairs) and "JSON array" can be 24 * constructed using a 'begin append* end' model. 25 * 26 * Nested objects and arrays can either be constructed bottom up (by 27 * creating sub object/arrays first and appending them to the super 28 * object/array) -or- by building them inline in one pass. This is a 29 * personal style and/or data shape choice. 30 * 31 * See t/helper/test-json-writer.c for various usage examples. 32 * 33 * LIMITATIONS: 34 * ============ 35 * 36 * The JSON specification [1,2] defines string values as Unicode data 37 * and probably UTF-8 encoded. The current json-writer API does not 38 * enforce this and will write any string as received. However, it will 39 * properly quote and backslash-escape them as necessary. It is up to 40 * the caller to UTF-8 encode their strings *before* passing them to this 41 * API. This layer should not have to try to guess the encoding or locale 42 * of the given strings. 43 */ 44 45struct json_writer 46{ 47/* 48 * Buffer of the in-progress JSON currently being composed. 49 */ 50struct strbuf json; 51 52/* 53 * Simple stack of the currently open array and object forms. 54 * This is a string of '{' and '[' characters indicating the 55 * currently unterminated forms. This is used to ensure the 56 * properly closing character is used when popping a level and 57 * to know when the JSON is completely closed. 58 */ 59struct strbuf open_stack; 60 61unsigned int need_comma:1; 62unsigned int pretty:1; 63}; 64 65#define JSON_WRITER_INIT { STRBUF_INIT, STRBUF_INIT, 0, 0 } 66 67voidjw_init(struct json_writer *jw); 68voidjw_release(struct json_writer *jw); 69 70voidjw_object_begin(struct json_writer *jw,int pretty); 71voidjw_array_begin(struct json_writer *jw,int pretty); 72 73voidjw_object_string(struct json_writer *jw,const char*key, 74const char*value); 75voidjw_object_intmax(struct json_writer *jw,const char*key,intmax_t value); 76voidjw_object_double(struct json_writer *jw,const char*key,int precision, 77double value); 78voidjw_object_true(struct json_writer *jw,const char*key); 79voidjw_object_false(struct json_writer *jw,const char*key); 80voidjw_object_bool(struct json_writer *jw,const char*key,int value); 81voidjw_object_null(struct json_writer *jw,const char*key); 82voidjw_object_sub_jw(struct json_writer *jw,const char*key, 83const struct json_writer *value); 84 85voidjw_object_inline_begin_object(struct json_writer *jw,const char*key); 86voidjw_object_inline_begin_array(struct json_writer *jw,const char*key); 87 88voidjw_array_string(struct json_writer *jw,const char*value); 89voidjw_array_intmax(struct json_writer *jw,intmax_t value); 90voidjw_array_double(struct json_writer *jw,int precision,double value); 91voidjw_array_true(struct json_writer *jw); 92voidjw_array_false(struct json_writer *jw); 93voidjw_array_bool(struct json_writer *jw,int value); 94voidjw_array_null(struct json_writer *jw); 95voidjw_array_sub_jw(struct json_writer *jw,const struct json_writer *value); 96voidjw_array_argc_argv(struct json_writer *jw,int argc,const char**argv); 97voidjw_array_argv(struct json_writer *jw,const char**argv); 98 99voidjw_array_inline_begin_object(struct json_writer *jw); 100voidjw_array_inline_begin_array(struct json_writer *jw); 101 102intjw_is_terminated(const struct json_writer *jw); 103voidjw_end(struct json_writer *jw); 104 105#endif/* JSON_WRITER_H */