29 #include "jsonParser.h"
31 #include "deviceUpdateMgrInternal.h"
37 static int parse_null(
void * ctx)
47 static int parse_boolean(
void * ctx,
int boolean)
50 parser->newBool(
boolean);
57 static int parse_number(
void * ctx,
const char * s,
size_t l)
62 parser->newString(str);
70 static int parse_string(
void * ctx,
const unsigned char * stringVal,
74 str.append((
const char *)stringVal, stringLen);
76 if(str==
"true" || str==
"false")
78 parser->newBool((str==
"true")?
true:
false);
81 parser->newString(str);
90 static int parse_map_key(
void * ctx,
const unsigned char * stringVal,
94 str.append((
const char *)stringVal, stringLen);
104 static int parse_start_map(
void * ctx)
106 yajl_gen g = (yajl_gen) ctx;
112 static int parse_end_map(
void * ctx)
114 yajl_gen g = (yajl_gen) ctx;
119 static int parse_start_array(
void * ctx)
121 yajl_gen g = (yajl_gen) ctx;
122 printf(
"got array start\n\r");
129 static int parse_end_array(
void * ctx)
131 yajl_gen g = (yajl_gen) ctx;
132 printf(
"got array end\n\r");
136 printf(
"exit array\n\r");
140 static yajl_callbacks callbacks = {
155 JSONParser::JSONParser()
161 JSONParser::~JSONParser()
165 std::map<string, JSONParser::varVal *> JSONParser::parse(
const unsigned char *json)
171 size_t jsonLen = strlen((
const char *)json);
174 yajl_gen_config config;
175 yajl_parser_config cfg;
179 g = yajl_gen_alloc(&config, NULL);
181 cfg.allowComments = 1;
183 hand = yajl_alloc(&callbacks, &cfg, NULL, (
void *)
this);
185 g = yajl_gen_alloc(NULL);
187 yajl_gen_config(g, yajl_gen_beautify, 1);
188 yajl_gen_config(g, yajl_gen_validate_utf8, 0);
190 hand = yajl_alloc(&callbacks, NULL, (
void *)
this);
192 yajl_config(hand, yajl_allow_comments, 1);
195 status = yajl_parse(hand, json, jsonLen);
197 if (status != yajl_status_ok) {
198 __TIMESTAMP();INT_LOG(
"JSONParser: Parse failed\n");
203 status = yajl_complete_parse(hand);
205 status = yajl_parse_complete(hand);
208 if (status != yajl_status_ok) {
209 unsigned char *errorString = yajl_get_error(hand, 1, json, jsonLen);
210 printf(
"%s", (
const char *) errorString);
211 yajl_free_error(hand, errorString);