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.
33 #include "api/yajl_parse.h"
35 #include "yajl_parser.h"
36 #include "yajl_alloc.h"
43 yajl_status_to_string(yajl_status stat
)
45 const char * statStr
= "unknown";
48 statStr
= "ok, no error";
50 case yajl_status_client_canceled
:
51 statStr
= "client canceled parse";
53 case yajl_status_insufficient_data
:
54 statStr
= "eof was met before the parse could complete";
56 case yajl_status_error
:
57 statStr
= "parse error";
64 yajl_alloc(const yajl_callbacks
* callbacks
,
65 const yajl_parser_config
* config
,
66 const yajl_alloc_funcs
* afs
,
69 unsigned int allowComments
= 0;
70 unsigned int validateUTF8
= 0;
71 yajl_handle hand
= NULL
;
72 yajl_alloc_funcs afsBuffer
;
74 /* first order of business is to set up memory allocation routines */
76 if (afs
->malloc
== NULL
|| afs
->realloc
== NULL
|| afs
->free
== NULL
)
81 yajl_set_default_alloc_funcs(&afsBuffer
);
85 hand
= (yajl_handle
) YA_MALLOC(afs
, sizeof(struct yajl_handle_t
));
87 /* copy in pointers to allocation routines */
88 memcpy((void *) &(hand
->alloc
), (void *) afs
, sizeof(yajl_alloc_funcs
));
91 allowComments
= config
->allowComments
;
92 validateUTF8
= config
->checkUTF8
;
95 hand
->callbacks
= callbacks
;
97 hand
->lexer
= yajl_lex_alloc(&(hand
->alloc
), allowComments
, validateUTF8
);
98 hand
->errorOffset
= 0;
99 hand
->decodeBuf
= yajl_buf_alloc(&(hand
->alloc
));
100 yajl_bs_init(hand
->stateStack
, &(hand
->alloc
));
102 yajl_bs_push(hand
->stateStack
, yajl_state_start
);
108 yajl_free(yajl_handle handle
)
110 yajl_bs_free(handle
->stateStack
);
111 yajl_buf_free(handle
->decodeBuf
);
112 yajl_lex_free(handle
->lexer
);
113 YA_FREE(&(handle
->alloc
), handle
);
117 yajl_parse(yajl_handle hand
, const unsigned char * jsonText
,
118 unsigned int jsonTextLen
)
120 unsigned int offset
= 0;
122 status
= yajl_do_parse(hand
, &offset
, jsonText
, jsonTextLen
);
127 yajl_parse_complete(yajl_handle hand
)
129 /* The particular case we want to handle is a trailing number.
130 * Further input consisting of digits could cause our interpretation
131 * of the number to change (buffered "1" but "2" comes in).
132 * A very simple approach to this is to inject whitespace to terminate
133 * any number in the lex buffer.
135 return yajl_parse(hand
, (const unsigned char *)" ", 1);
139 yajl_get_error(yajl_handle hand
, int verbose
,
140 const unsigned char * jsonText
, unsigned int jsonTextLen
)
142 return yajl_render_error_string(hand
, jsonText
, jsonTextLen
, verbose
);
146 yajl_free_error(yajl_handle hand
, unsigned char * str
)
148 /* use memory allocation functions if set */
149 YA_FREE(&(hand
->alloc
), str
);
152 /* XXX: add utility routines to parse from file */