| 1 | /* |
| 2 | * Copyright (c) 2007-2014, Lloyd Hilaiel <[email protected]> |
| 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 | /** |
| 18 | * \file yajl_gen.h |
| 19 | * Interface to YAJL's JSON generation facilities. |
| 20 | */ |
| 21 | |
| 22 | #include <yajl/api/yajl_common.h> |
| 23 | |
| 24 | #ifndef __YAJL_GEN_H__ |
| 25 | #define __YAJL_GEN_H__ |
| 26 | |
| 27 | #include <stddef.h> |
| 28 | |
| 29 | #ifdef __cplusplus |
| 30 | extern "C" { |
| 31 | #endif |
| 32 | /** generator status codes */ |
| 33 | typedef enum { |
| 34 | /** no error */ |
| 35 | yajl_gen_status_ok = 0, |
| 36 | /** at a point where a map key is generated, a function other than |
| 37 | * yajl_gen_string was called */ |
| 38 | yajl_gen_keys_must_be_strings, |
| 39 | /** YAJL's maximum generation depth was exceeded. see |
| 40 | * YAJL_MAX_DEPTH */ |
| 41 | yajl_max_depth_exceeded, |
| 42 | /** A generator function (yajl_gen_XXX) was called while in an error |
| 43 | * state */ |
| 44 | yajl_gen_in_error_state, |
| 45 | /** A complete JSON document has been generated */ |
| 46 | yajl_gen_generation_complete, |
| 47 | /** yajl_gen_double was passed an invalid floating point value |
| 48 | * (infinity or NaN). */ |
| 49 | yajl_gen_invalid_number, |
| 50 | /** A print callback was passed in, so there is no internal |
| 51 | * buffer to get from */ |
| 52 | yajl_gen_no_buf, |
| 53 | /** returned from yajl_gen_string() when the yajl_gen_validate_utf8 |
| 54 | * option is enabled and an invalid was passed by client code. |
| 55 | */ |
| 56 | yajl_gen_invalid_string |
| 57 | } yajl_gen_status; |
| 58 | |
| 59 | /** an opaque handle to a generator */ |
| 60 | typedef struct yajl_gen_t * yajl_gen; |
| 61 | |
| 62 | /** a callback used for "printing" the results. */ |
| 63 | typedef void (*yajl_print_t)(void * ctx, |
| 64 | const char * str, |
| 65 | size_t len); |
| 66 | |
| 67 | /** configuration parameters for the parser, these may be passed to |
| 68 | * yajl_gen_config() along with option specific argument(s). In general, |
| 69 | * all configuration parameters default to *off*. */ |
| 70 | typedef enum { |
| 71 | /** generate indented (beautiful) output */ |
| 72 | yajl_gen_beautify = 0x01, |
| 73 | /** |
| 74 | * Set an indent string which is used when yajl_gen_beautify |
| 75 | * is enabled. Maybe something like \\t or some number of |
| 76 | * spaces. The default is four spaces ' '. |
| 77 | */ |
| 78 | yajl_gen_indent_string = 0x02, |
| 79 | /** |
| 80 | * Set a function and context argument that should be used to |
| 81 | * output generated json. the function should conform to the |
| 82 | * yajl_print_t prototype while the context argument is a |
| 83 | * void * of your choosing. |
| 84 | * |
| 85 | * example: |
| 86 | * yajl_gen_config(g, yajl_gen_print_callback, myFunc, myVoidPtr); |
| 87 | */ |
| 88 | yajl_gen_print_callback = 0x04, |
| 89 | /** |
| 90 | * Normally the generator does not validate that strings you |
| 91 | * pass to it via yajl_gen_string() are valid UTF8. Enabling |
| 92 | * this option will cause it to do so. |
| 93 | */ |
| 94 | yajl_gen_validate_utf8 = 0x08, |
| 95 | /** |
| 96 | * the forward solidus (slash or '/' in human) is not required to be |
| 97 | * escaped in json text. By default, YAJL will not escape it in the |
| 98 | * iterest of saving bytes. Setting this flag will cause YAJL to |
| 99 | * always escape '/' in generated JSON strings. |
| 100 | */ |
| 101 | yajl_gen_escape_solidus = 0x10 |
| 102 | } yajl_gen_option; |
| 103 | |
| 104 | /** allow the modification of generator options subsequent to handle |
| 105 | * allocation (via yajl_alloc) |
| 106 | * \returns zero in case of errors, non-zero otherwise |
| 107 | */ |
| 108 | YAJL_API int yajl_gen_config(yajl_gen g, yajl_gen_option opt, ...); |
| 109 | |
| 110 | /** allocate a generator handle |
| 111 | * \param allocFuncs an optional pointer to a structure which allows |
| 112 | * the client to overide the memory allocation |
| 113 | * used by yajl. May be NULL, in which case |
| 114 | * malloc/free/realloc will be used. |
| 115 | * |
| 116 | * \returns an allocated handle on success, NULL on failure (bad params) |
| 117 | */ |
| 118 | YAJL_API yajl_gen yajl_gen_alloc(const yajl_alloc_funcs * allocFuncs); |
| 119 | |
| 120 | /** free a generator handle */ |
| 121 | YAJL_API void yajl_gen_free(yajl_gen handle); |
| 122 | |
| 123 | YAJL_API yajl_gen_status yajl_gen_integer(yajl_gen hand, long long int number); |
| 124 | /** generate a floating point number. number may not be infinity or |
| 125 | * NaN, as these have no representation in JSON. In these cases the |
| 126 | * generator will return 'yajl_gen_invalid_number' */ |
| 127 | YAJL_API yajl_gen_status yajl_gen_double(yajl_gen hand, double number); |
| 128 | YAJL_API yajl_gen_status yajl_gen_number(yajl_gen hand, |
| 129 | const char * num, |
| 130 | size_t len); |
| 131 | YAJL_API yajl_gen_status yajl_gen_string(yajl_gen hand, |
| 132 | const unsigned char * str, |
| 133 | size_t len); |
| 134 | YAJL_API yajl_gen_status yajl_gen_null(yajl_gen hand); |
| 135 | YAJL_API yajl_gen_status yajl_gen_bool(yajl_gen hand, int boolean); |
| 136 | YAJL_API yajl_gen_status yajl_gen_map_open(yajl_gen hand); |
| 137 | YAJL_API yajl_gen_status yajl_gen_map_close(yajl_gen hand); |
| 138 | YAJL_API yajl_gen_status yajl_gen_array_open(yajl_gen hand); |
| 139 | YAJL_API yajl_gen_status yajl_gen_array_close(yajl_gen hand); |
| 140 | |
| 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 | |
| 148 | /** clear yajl's output buffer, but maintain all internal generation |
| 149 | * state. This function will not "reset" the generator state, and is |
| 150 | * intended to enable incremental JSON outputing. */ |
| 151 | YAJL_API void yajl_gen_clear(yajl_gen hand); |
| 152 | |
| 153 | /** Reset the generator state. Allows a client to generate multiple |
| 154 | * json entities in a stream. The "sep" string will be inserted to |
| 155 | * separate the previously generated entity from the current, |
| 156 | * NULL means *no separation* of entites (clients beware, generating |
| 157 | * multiple JSON numbers without a separator, for instance, will result in ambiguous output) |
| 158 | * |
| 159 | * Note: this call will not clear yajl's output buffer. This |
| 160 | * may be accomplished explicitly by calling yajl_gen_clear() */ |
| 161 | YAJL_API void yajl_gen_reset(yajl_gen hand, const char * sep); |
| 162 | |
| 163 | #ifdef __cplusplus |
| 164 | } |
| 165 | #endif |
| 166 | |
| 167 | #endif |