yajl_gen.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2007-2014, Lloyd Hilaiel <me@lloyd.io>
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /**
  17. * \file yajl_gen.h
  18. * Interface to YAJL's JSON generation facilities.
  19. */
  20. #include "yajl_common.h"
  21. #ifndef __YAJL_GEN_H__
  22. #define __YAJL_GEN_H__
  23. #include <stddef.h>
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /** generator status codes */
  28. typedef enum {
  29. /** no error */
  30. yajl_gen_status_ok = 0,
  31. /** at a point where a map key is generated, a function other than
  32. * yajl_gen_string was called */
  33. yajl_gen_keys_must_be_strings,
  34. /** YAJL's maximum generation depth was exceeded. see
  35. * YAJL_MAX_DEPTH */
  36. yajl_max_depth_exceeded,
  37. /** A generator function (yajl_gen_XXX) was called while in an error
  38. * state */
  39. yajl_gen_in_error_state,
  40. /** A complete JSON document has been generated */
  41. yajl_gen_generation_complete,
  42. /** yajl_gen_double was passed an invalid floating point value
  43. * (infinity or NaN). */
  44. yajl_gen_invalid_number,
  45. /** A print callback was passed in, so there is no internal
  46. * buffer to get from */
  47. yajl_gen_no_buf,
  48. /** returned from yajl_gen_string() when the yajl_gen_validate_utf8
  49. * option is enabled and an invalid was passed by client code.
  50. */
  51. yajl_gen_invalid_string
  52. } yajl_gen_status;
  53. /** an opaque handle to a generator */
  54. typedef struct yajl_gen_t * yajl_gen;
  55. /** a callback used for "printing" the results. */
  56. typedef void (*yajl_print_t)(void * ctx,
  57. const char * str,
  58. size_t len);
  59. /** configuration parameters for the parser, these may be passed to
  60. * yajl_gen_config() along with option specific argument(s). In general,
  61. * all configuration parameters default to *off*. */
  62. typedef enum {
  63. /** generate indented (beautiful) output */
  64. yajl_gen_beautify = 0x01,
  65. /**
  66. * Set an indent string which is used when yajl_gen_beautify
  67. * is enabled. Maybe something like \\t or some number of
  68. * spaces. The default is four spaces ' '.
  69. */
  70. yajl_gen_indent_string = 0x02,
  71. /**
  72. * Set a function and context argument that should be used to
  73. * output generated json. the function should conform to the
  74. * yajl_print_t prototype while the context argument is a
  75. * void * of your choosing.
  76. *
  77. * example:
  78. * yajl_gen_config(g, yajl_gen_print_callback, myFunc, myVoidPtr);
  79. */
  80. yajl_gen_print_callback = 0x04,
  81. /**
  82. * Normally the generator does not validate that strings you
  83. * pass to it via yajl_gen_string() are valid UTF8. Enabling
  84. * this option will cause it to do so.
  85. */
  86. yajl_gen_validate_utf8 = 0x08,
  87. /**
  88. * the forward solidus (slash or '/' in human) is not required to be
  89. * escaped in json text. By default, YAJL will not escape it in the
  90. * iterest of saving bytes. Setting this flag will cause YAJL to
  91. * always escape '/' in generated JSON strings.
  92. */
  93. yajl_gen_escape_solidus = 0x10,
  94. /**
  95. * Disable yandex double format
  96. */
  97. yajl_gen_disable_yandex_double_format = 0x20,
  98. /**
  99. * do not print final newline '\n' in case of indented (beautiful) output
  100. */
  101. yajl_gen_skip_final_newline = 0x40,
  102. /**
  103. * enable printing infinity value
  104. */
  105. yajl_gen_support_infinity = 0x80
  106. } yajl_gen_option;
  107. /** allow the modification of generator options subsequent to handle
  108. * allocation (via yajl_alloc)
  109. * \returns zero in case of errors, non-zero otherwise
  110. */
  111. YAJL_API int yajl_gen_config(yajl_gen g, yajl_gen_option opt, ...);
  112. /** allocate a generator handle
  113. * \param allocFuncs an optional pointer to a structure which allows
  114. * the client to overide the memory allocation
  115. * used by yajl. May be NULL, in which case
  116. * malloc/free/realloc will be used.
  117. *
  118. * \returns an allocated handle on success, NULL on failure (bad params)
  119. */
  120. YAJL_API yajl_gen yajl_gen_alloc(const yajl_alloc_funcs * allocFuncs);
  121. /** free a generator handle */
  122. YAJL_API void yajl_gen_free(yajl_gen handle);
  123. YAJL_API yajl_gen_status yajl_gen_integer(yajl_gen hand, long long int number);
  124. YAJL_API yajl_gen_status yajl_gen_uinteger(yajl_gen hand, long long unsigned number);
  125. /** generate a floating point number. number may not be infinity or
  126. * NaN, as these have no representation in JSON. In these cases the
  127. * generator will return 'yajl_gen_invalid_number' */
  128. YAJL_API yajl_gen_status yajl_gen_double(yajl_gen hand, double number);
  129. YAJL_API yajl_gen_status yajl_gen_number(yajl_gen hand,
  130. const char * num,
  131. size_t len);
  132. YAJL_API yajl_gen_status yajl_gen_string(yajl_gen hand,
  133. const unsigned char * str,
  134. size_t len);
  135. YAJL_API yajl_gen_status yajl_gen_null(yajl_gen hand);
  136. YAJL_API yajl_gen_status yajl_gen_bool(yajl_gen hand, int boolean);
  137. YAJL_API yajl_gen_status yajl_gen_map_open(yajl_gen hand);
  138. YAJL_API yajl_gen_status yajl_gen_map_close(yajl_gen hand);
  139. YAJL_API yajl_gen_status yajl_gen_array_open(yajl_gen hand);
  140. YAJL_API yajl_gen_status yajl_gen_array_close(yajl_gen hand);
  141. /** access the null terminated generator buffer. If incrementally
  142. * outputing JSON, one should call yajl_gen_clear to clear the
  143. * buffer. This allows stream generation. */
  144. YAJL_API yajl_gen_status yajl_gen_get_buf(yajl_gen hand,
  145. const unsigned char ** buf,
  146. size_t * len);
  147. /** clear yajl's output buffer, but maintain all internal generation
  148. * state. This function will not "reset" the generator state, and is
  149. * intended to enable incremental JSON outputing. */
  150. YAJL_API void yajl_gen_clear(yajl_gen hand);
  151. /** Reset the generator state. Allows a client to generate multiple
  152. * json entities in a stream. The "sep" string will be inserted to
  153. * separate the previously generated entity from the current,
  154. * NULL means *no separation* of entites (clients beware, generating
  155. * multiple JSON numbers without a separator, for instance, will result in ambiguous output)
  156. *
  157. * Note: this call will not clear yajl's output buffer. This
  158. * may be accomplished explicitly by calling yajl_gen_clear() */
  159. YAJL_API void yajl_gen_reset(yajl_gen hand, const char * sep);
  160. #ifdef __cplusplus
  161. }
  162. #endif
  163. #endif