You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

json-writer.h 4.2KB

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