29 #include "jsonParser.h"
36 static int parse_null(
void * ctx)
46 static int parse_boolean(
void * ctx,
int boolean)
49 parser->newBool(
boolean);
56 static int parse_number(
void * ctx,
const char * s,
size_t l)
61 parser->newString(str);
69 static int parse_string(
void * ctx,
const unsigned char * stringVal,
73 str.append((
const char *)stringVal, stringLen);
75 if(str==
"true" || str==
"false")
77 parser->newBool((str==
"true")?
true:
false);
80 parser->newString(str);
89 static int parse_map_key(
void * ctx,
const unsigned char * stringVal,
93 str.append((
const char *)stringVal, stringLen);
103 static int parse_start_map(
void * ctx)
105 yajl_gen g = (yajl_gen) ctx;
111 static int parse_end_map(
void * ctx)
113 yajl_gen g = (yajl_gen) ctx;
118 static int parse_start_array(
void * ctx)
120 yajl_gen g = (yajl_gen) ctx;
128 static int parse_end_array(
void * ctx)
130 yajl_gen g = (yajl_gen) ctx;
138 static yajl_callbacks callbacks = {
153 JSONParser::JSONParser()
159 JSONParser::~JSONParser()
163 std::map<string, JSONParser::varVal *> JSONParser::parse(
const unsigned char *json)
168 yajl_gen_config config;
169 size_t jsonLen = strlen((
const char *)json);
171 yajl_parser_config cfg;
175 g = yajl_gen_alloc(&config, NULL);
178 cfg.allowComments = 1;
180 hand = yajl_alloc(&callbacks, &cfg, NULL, (
void *)
this);
181 status = yajl_parse(hand, json, jsonLen);
183 if (status != yajl_status_ok) {
184 __TIMESTAMP();printf(
"JSONParser: Parse failed\n");
188 status = yajl_parse_complete(hand);
189 if (status != yajl_status_ok) {
190 unsigned char *errorString = yajl_get_error(hand, 1, json, jsonLen);
191 printf(
"%s", (
const char *) errorString);
192 yajl_free_error(hand, errorString);