commit | author | age
|
6e1425
|
1 |
/* |
H |
2 |
* Copyright 2007-2009, Lloyd Hilaiel. |
|
3 |
* |
|
4 |
* Redistribution and use in source and binary forms, with or without |
|
5 |
* modification, are permitted provided that the following conditions are |
|
6 |
* met: |
|
7 |
* |
|
8 |
* 1. Redistributions of source code must retain the above copyright |
|
9 |
* notice, this list of conditions and the following disclaimer. |
|
10 |
* |
|
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 |
|
14 |
* distribution. |
|
15 |
* |
|
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. |
|
19 |
* |
|
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. |
|
31 |
*/ |
|
32 |
|
|
33 |
/** |
|
34 |
* \file yajl_gen.h |
|
35 |
* Interface to YAJL's JSON generation facilities. |
|
36 |
*/ |
|
37 |
|
|
38 |
#include "yajl_common.h" |
|
39 |
|
|
40 |
#ifndef __YAJL_GEN_H__ |
|
41 |
#define __YAJL_GEN_H__ |
|
42 |
|
|
43 |
#ifdef __cplusplus |
|
44 |
extern "C" { |
|
45 |
#endif |
|
46 |
/** generator status codes */ |
|
47 |
typedef enum { |
|
48 |
/** no error */ |
|
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 |
|
54 |
* YAJL_MAX_DEPTH */ |
|
55 |
yajl_max_depth_exceeded, |
|
56 |
/** A generator function (yajl_gen_XXX) was called while in an error |
|
57 |
* state */ |
|
58 |
yajl_gen_in_error_state, |
|
59 |
/** A complete JSON document has been generated */ |
|
60 |
yajl_gen_generation_complete, |
|
61 |
/** yajl_gen_double was passed an invalid floating point value |
|
62 |
* (infinity or NaN). */ |
|
63 |
yajl_gen_invalid_number, |
|
64 |
/** A print callback was passed in, so there is no internal |
|
65 |
* buffer to get from */ |
|
66 |
yajl_gen_no_buf |
|
67 |
} yajl_gen_status; |
|
68 |
|
|
69 |
/** an opaque handle to a generator */ |
|
70 |
typedef struct yajl_gen_t * yajl_gen; |
|
71 |
|
|
72 |
/** a callback used for "printing" the results. */ |
|
73 |
typedef void (*yajl_print_t)(void * ctx, |
|
74 |
const char * str, |
|
75 |
unsigned int len); |
|
76 |
|
|
77 |
/** configuration structure for the generator */ |
|
78 |
typedef struct { |
|
79 |
/** generate indented (beautiful) output */ |
|
80 |
unsigned int beautify; |
|
81 |
/** an opportunity to define an indent string. such as \\t or |
|
82 |
* some number of spaces. default is four spaces ' '. This |
|
83 |
* member is only relevant when beautify is true */ |
|
84 |
const char * indentString; |
|
85 |
} yajl_gen_config; |
|
86 |
|
|
87 |
/** allocate a generator handle |
|
88 |
* \param config a pointer to a structure containing parameters which |
|
89 |
* configure the behavior of the json generator |
|
90 |
* \param allocFuncs an optional pointer to a structure which allows |
|
91 |
* the client to overide the memory allocation |
|
92 |
* used by yajl. May be NULL, in which case |
|
93 |
* malloc/free/realloc will be used. |
|
94 |
* |
|
95 |
* \returns an allocated handle on success, NULL on failure (bad params) |
|
96 |
*/ |
|
97 |
YAJL_API yajl_gen yajl_gen_alloc(const yajl_gen_config * config, |
|
98 |
const yajl_alloc_funcs * allocFuncs); |
|
99 |
|
|
100 |
/** allocate a generator handle that will print to the specified |
|
101 |
* callback rather than storing the results in an internal buffer. |
|
102 |
* \param callback a pointer to a printer function. May be NULL |
|
103 |
* in which case, the results will be store in an |
|
104 |
* internal buffer. |
|
105 |
* \param config a pointer to a structure containing parameters |
|
106 |
* which configure the behavior of the json |
|
107 |
* generator. |
|
108 |
* \param allocFuncs an optional pointer to a structure which allows |
|
109 |
* the client to overide the memory allocation |
|
110 |
* used by yajl. May be NULL, in which case |
|
111 |
* malloc/free/realloc will be used. |
|
112 |
* \param ctx a context pointer that will be passed to the |
|
113 |
* printer callback. |
|
114 |
* |
|
115 |
* \returns an allocated handle on success, NULL on failure (bad params) |
|
116 |
*/ |
|
117 |
YAJL_API yajl_gen yajl_gen_alloc2(const yajl_print_t callback, |
|
118 |
const yajl_gen_config * config, |
|
119 |
const yajl_alloc_funcs * allocFuncs, |
|
120 |
void * ctx); |
|
121 |
|
|
122 |
/** free a generator handle */ |
|
123 |
YAJL_API void yajl_gen_free(yajl_gen handle); |
|
124 |
|
|
125 |
YAJL_API yajl_gen_status yajl_gen_integer(yajl_gen hand, long int number); |
|
126 |
/** generate a floating point number. number may not be infinity or |
|
127 |
* NaN, as these have no representation in JSON. In these cases the |
|
128 |
* generator will return 'yajl_gen_invalid_number' */ |
|
129 |
YAJL_API yajl_gen_status yajl_gen_double(yajl_gen hand, double number); |
|
130 |
YAJL_API yajl_gen_status yajl_gen_number(yajl_gen hand, |
|
131 |
const char * num, |
|
132 |
unsigned int len); |
|
133 |
YAJL_API yajl_gen_status yajl_gen_string(yajl_gen hand, |
|
134 |
const unsigned char * str, |
|
135 |
unsigned int len); |
|
136 |
YAJL_API yajl_gen_status yajl_gen_null(yajl_gen hand); |
|
137 |
YAJL_API yajl_gen_status yajl_gen_bool(yajl_gen hand, int boolean); |
|
138 |
YAJL_API yajl_gen_status yajl_gen_map_open(yajl_gen hand); |
|
139 |
YAJL_API yajl_gen_status yajl_gen_map_close(yajl_gen hand); |
|
140 |
YAJL_API yajl_gen_status yajl_gen_array_open(yajl_gen hand); |
|
141 |
YAJL_API yajl_gen_status yajl_gen_array_close(yajl_gen hand); |
|
142 |
|
|
143 |
/** access the null terminated generator buffer. If incrementally |
|
144 |
* outputing JSON, one should call yajl_gen_clear to clear the |
|
145 |
* buffer. This allows stream generation. */ |
|
146 |
YAJL_API yajl_gen_status yajl_gen_get_buf(yajl_gen hand, |
|
147 |
const unsigned char ** buf, |
|
148 |
unsigned int * len); |
|
149 |
|
|
150 |
/** clear yajl's output buffer, but maintain all internal generation |
|
151 |
* state. This function will not "reset" the generator state, and is |
|
152 |
* intended to enable incremental JSON outputing. */ |
|
153 |
YAJL_API void yajl_gen_clear(yajl_gen hand); |
|
154 |
|
|
155 |
#ifdef __cplusplus |
|
156 |
} |
|
157 |
#endif |
|
158 |
|
|
159 |
#endif |