2 * Copyright 2007-2009, Lloyd Hilaiel.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. Neither the name of Lloyd Hilaiel nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
35 * Interface to YAJL's JSON generation facilities.
38 #include <yajl/yajl_common.h>
40 #ifndef __YAJL_GEN_H__
41 #define __YAJL_GEN_H__
46 /** generator status codes */
49 yajl_gen_status_ok
= 0,
50 /** at a point where a map key is generated, a function other than
51 * yajl_gen_string was called */
52 yajl_gen_keys_must_be_strings
,
53 /** YAJL's maximum generation depth was exceeded. see
55 yajl_max_depth_exceeded
,
56 /** A generator function (yajl_gen_XXX) was called while in an error
58 yajl_gen_in_error_state
,
59 /** A complete JSON document has been generated */
60 yajl_gen_generation_complete
63 /** an opaque handle to a generator */
64 typedef struct yajl_gen_t
* yajl_gen
;
66 /** configuration structure for the generator */
68 /** generate indented (beautiful) output */
69 unsigned int beautify
;
70 /** an opportunity to define an indent string. such as \\t or
71 * some number of spaces. default is four spaces ' '. This
72 * member is only relevant when beautify is true */
73 const char * indentString
;
76 /** allocate a generator handle
77 * \param config a pointer to a structure containing parameters which
78 * configure the behavior of the json generator
79 * \param allocFuncs an optional pointer to a structure which allows
80 * the client to overide the memory allocation
81 * used by yajl. May be NULL, in which case
82 * malloc/free/realloc will be used.
84 * \returns an allocated handle on success, NULL on failure (bad params)
86 yajl_gen YAJL_API
yajl_gen_alloc(const yajl_gen_config
* config
,
87 const yajl_alloc_funcs
* allocFuncs
);
89 /** free a generator handle */
90 void YAJL_API
yajl_gen_free(yajl_gen handle
);
92 yajl_gen_status YAJL_API
yajl_gen_integer(yajl_gen hand
, long int number
);
93 yajl_gen_status YAJL_API
yajl_gen_double(yajl_gen hand
, double number
);
94 yajl_gen_status YAJL_API
yajl_gen_number(yajl_gen hand
,
97 yajl_gen_status YAJL_API
yajl_gen_string(yajl_gen hand
,
98 const unsigned char * str
,
100 yajl_gen_status YAJL_API
yajl_gen_null(yajl_gen hand
);
101 yajl_gen_status YAJL_API
yajl_gen_bool(yajl_gen hand
, int boolean
);
102 yajl_gen_status YAJL_API
yajl_gen_map_open(yajl_gen hand
);
103 yajl_gen_status YAJL_API
yajl_gen_map_close(yajl_gen hand
);
104 yajl_gen_status YAJL_API
yajl_gen_array_open(yajl_gen hand
);
105 yajl_gen_status YAJL_API
yajl_gen_array_close(yajl_gen hand
);
107 /** access the null terminated generator buffer. If incrementally
108 * outputing JSON, one should call yajl_gen_clear to clear the
109 * buffer. This allows stream generation. */
110 yajl_gen_status YAJL_API
yajl_gen_get_buf(yajl_gen hand
,
111 const unsigned char ** buf
,
114 /** clear yajl's output buffer, but maintain all internal generation
115 * state. This function will not "reset" the generator state, and is
116 * intended to enable incremental JSON outputing. */
117 void YAJL_API
yajl_gen_clear(yajl_gen hand
);